Powershell Import-Pssession在自定义模块中使用时不导入cmdlet

Powershell Import-Pssession在自定义模块中使用时不导入cmdlet,powershell,powershell-3.0,Powershell,Powershell 3.0,我有一个PowerShell脚本/函数,当我在我的PowerShell配置文件中使用它或在PowerShell窗口中手动复制/粘贴该函数时,它工作得非常好 我正试图让我的团队中的其他成员可以作为一个模块访问该函数。我想将模块存储在一个中心位置,以便我们都可以将其添加到PSModulePath 以下是基本功能的副本: Function Connect-O365{ $o365cred = Get-Credential username@domain.onmicrosoft.com $

我有一个PowerShell脚本/函数,当我在我的PowerShell配置文件中使用它或在PowerShell窗口中手动复制/粘贴该函数时,它工作得非常好

我正试图让我的团队中的其他成员可以作为一个模块访问该函数。我想将模块存储在一个中心位置,以便我们都可以将其添加到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脚本中添加这个函数,它也可以工作

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

我正在寻找关于如何解决这个问题的建议

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

$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
$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
当我在不使用Connect-O365函数的情况下导入下一个模块时,导入的cmdlet可用

$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
$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

这似乎是某种范围问题,只是不确定如何解决。

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

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
}

谢天谢地,我两个问答都能+1。谢谢你。我建议你用马克作为答案。