我有以下代码可以使用 powershell 连接到我的 office 365 帐户:
$Cred=GET-CREDENTIAL
Write-Host "Connecting..."
IMPORT-MODULE MSONLINE
CONNECT-MSOLService -credential $Cred
$s = NEW-PSSESSION -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection
$importresults=import-pssession $s
Write-Host "Connected to exchange server"
但是由于这有效地连接了两次,一次使用 new-pssession,一次使用 connect -MSOLService,因此应该可以同时进行这两个操作,例如:
$Cred=GET-CREDENTIAL
Write-Host "Connecting..."
IMPORT-MODULE MSONLINE
$j = start-job -scriptBlock { CONNECT-MSOLService -credential $Cred }
$s = NEW-PSSESSION -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection
$importresults=import-pssession $s
wait-job $j
Write-Host "Connected to exchange server"
但这实际上不起作用(我猜这是变量范围的问题?这可能吗/我应该怎么做?