我有下面的代碼使用PowerShell連接到我的Office 365帳戶:連接到Exchange asyncronously使用PowerShell
$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"
但由於這有效地連接兩次,一次是與新的PSSession和曾經與連接-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"
但是,這並不實際工作(我猜它是與變量的作用域的問題,這是可以做到的/我應該如何去做吧?
然後結果有狀態失敗和HasMoreData錯誤(它不起作用)。在我完成狀態並且HasMoreData爲該作業之前。 – ForbesLindesay
這是在開始工作時傳遞參數的方法。可以幫助更多,因爲我可以測試它。抱歉 –