开发者

Connect to exchange asyncronously using powershell

开发者 https://www.devze.com 2023-04-08 16:56 出处:网络
I have the following code to connect to my office 365 account using powershell: $Cred=GET-CREDENTIAL Write-Host \"Connecting...\"

I have the following code to connect to my office 365 account using powershell:

$Cred=GET-CREDENTIAL 
Write-Host "Connecting..."
IMP开发者_如何学GoORT-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"

but since this effectively connects twice, once with new-pssession and once with connect -MSOLService, it ought to be possible to do both simultaneously, e.g.:

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

But this doesn't actually work (I'm guessing it's an issue with the scope of the variables? Is this possible to do/how should I do it?


try this:

Start-Job -scriptblock {Param ($cred) CONNECT-MSOLService -credential $Cred} -ArgumentList $cred


I've come to the conclusion this probably isn't possible. I believe the problem is that the login commands modify the context they run in but the context is different if they are done inside an asynchronous job.

0

精彩评论

暂无评论...
验证码 换一张
取 消