在自定义模块中使用时,Import-Pssession 不导入 cmdlet [英] Import-Pssession is not importing cmdlets when used in a custom module

查看:46
本文介绍了在自定义模块中使用时,Import-Pssession 不导入 cmdlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PowerShell 脚本/函数,当我在我的 PowerShell 配置文件中使用它或在 PowerShell 窗口中手动复制/粘贴该函数时效果很好.

I have a PowerShell script/function that works great when I use it in my PowerShell profile or manually copy/paste the function in the PowerShell window.

我试图让我的团队的其他成员可以访问该功能作为一个模块.我希望将模块存储在一个中央位置,以便我们都可以将其添加到我们的 PSModulePath 中.

I'm trying to make the function accessible to other members of my team as a module. I want to have the module stored in a central place so we can all add it to our PSModulePath.

这是基本功能的副本:

Function Connect-O365{
    $o365cred = Get-Credential username@domain.onmicrosoft.com
    $session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $o365cred -Authentication Basic -AllowRedirection
    Import-PSSession $session365 -AllowClobber
}

如果我将此函数保存在我的 PowerShell 配置文件中,它就可以正常工作.我可以用这个函数来点源 *.ps1 脚本,它也能正常工作.

If I save this function in my PowerShell profile it works fine. I can dot source a *.ps1 script with this function in it and it works as well.

问题是当我将函数保存为 *.psm1 PowerShell 脚本模块时.该函数运行良好,但从 Import-PSSession 导出的命令均不可用.我认为这可能与模块范围有关.

The issue is when I save the function as a *.psm1 PowerShell script module. The function runs fine but none of the exported commands from the Import-PSSession are available. I think this may have something to do with the module scope.

我正在寻找有关如何解决此问题的建议.

I'm looking for suggestions on how to get around this.

当我创建以下模块并运行 Connect-O365 时,导入的 cmdlet 将不可用.

When I create the following module and run Connect-O365 the imported cmdlets will not be available.

$scriptblock = {
    Function Connect-O365 {
        $o365cred = Get-Credential username@domain.onmicrosoft.com
        $session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $o365cred -Authentication Basic -AllowRedirection
        Import-PSSession $session365 -AllowClobber
    }
}

New-Module -Name "Office 365" -ScriptBlock $scriptblock

当我在没有 Connect-O365 函数的情况下导入下一个模块时,导入的 cmdlet 可用.

When I import the next module without the Connect-O365 function the imported cmdlets are available.

$scriptblock = {
    $o365cred = Get-Credential username@domain.onmicrosoft.com
    $session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $o365cred -Authentication Basic -AllowRedirection
    Import-PSSession $session365 -AllowClobber
}

New-Module -Name "Office 365" -ScriptBlock $scriptblock

这似乎是某种范围的问题,只是不知道如何解决.

This appears to be a scope issue of some sort, just not sure how to get around it.

推荐答案

在 TechNet 的帮助下,我能够修改脚本模块,使其按我预期的方式工作.

With some assistance from TechNet I was able to modify the script module so it worked the way I expected.

function Connect-O365 {
    $o365cred = Get-Credential username@domain.onmicrosoft.com
    $session365 = New-PSSession `
                    -ConfigurationName Microsoft.Exchange `
                    -ConnectionUri "https://ps.outlook.com/powershell/" `
                    -Credential $o365cred `
                    -Authentication Basic `
                    -AllowRedirection 
    Import-Module (Import-PSSession $session365 -AllowClobber) -Global
}

TechNet 帖子

这篇关于在自定义模块中使用时,Import-Pssession 不导入 cmdlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆