我正在尝试在模块上创建一个函数,该函数将创建一个PsSession并将其导入到我的控制台中。脚本块本身还可以,但是运行cmdlet之后,尽管函数将其导入,但我无法导入PsSession。
Function ConnectTo-Office365 {
[cmdletbinding()]
$365Credential = Get-Credential -Message "Office365 Credentials";
$365Session = New-PSSession -ConfigurationName Microsoft.Exchange ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $365Credential -Authentication Basic -AllowRedirection;
Import-PSSession $365Session}
我已经阅读了全局变量,但不了解如何将其用于我的目的。
谢谢!
答案 0 :(得分:0)
您不需要使用全局变量。
Function ConnectTo-Office365 {
[cmdletbinding()]
Param (
$Credential = (Get-Credential -Message "Office365 Credentials")
)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $Credential -Authentication Basic -AllowRedirection
Return $Session
}
PS> Import-PSSession (ConnectTo-Office365)
PS> Import-PSSession (ConnectTo-Office356 -Credential $Cred)