使用 powershell 异步连接以进行交换
我有以下代码使用 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"
但这并不'实际上不起作用(我猜这是变量范围的问题?这可能吗/我应该怎么做?
I have the following code to connect to my office 365 account using 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"
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
try this:
我得出的结论是这可能是不可能的。我认为问题在于登录命令修改了它们运行的上下文,但如果它们是在异步作业中完成的,则上下文会有所不同。
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.