0

我有以下代码可以使用 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"

但这实际上不起作用(我猜这是变量范围的问题?这可能吗/我应该怎么做?

4

2 回答 2

1

试试这个:

Start-Job -scriptblock {Param ($cred) CONNECT-MSOLService -credential $Cred} -ArgumentList $cred
于 2011-09-26T14:23:08.163 回答
1

我得出结论,这可能是不可能的。我认为问题在于登录命令会修改它们运行的​​上下文,但如果它们是在异步作业中完成的,则上下文会有所不同。

于 2012-02-14T14:21:52.570 回答