在我们的脚本可以继续之前,我们有两个 PSSession 需要建立并导入到当前会话中。两个步骤都需要大约 10 - 15 秒,串联运行时总共需要 20 - 30 秒。
是否可以在单独的运行空间中运行 New-PSSession,然后以某种方式将该已建立的会话导入父进程?
例如从此改变:
New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri ("https://$($service)/PowerShell/") -Credential $Credential -Authentication Basic -AllowRedirection -ErrorAction Stop
New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $Credential -Authentication Basic -AllowRedirection -ErrorAction Stop
可能是这样的(警告这不起作用):
$credential = Get-Credential
$scriptblock =
{
param ([string]$Credential)
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $Credential -Authentication Basic -AllowRedirection -ErrorAction Stop
return $session
}
$shell = [PowerShell]::Create().AddScript($scriptblock).AddParameter($credential)
$job = $shell.BeginInvoke()
$result = $shell.EndInvoke($job)
Import-PSSession $result
最终目标是让这花费更少的时间,如果我们并行使用 New-PSSession,它会在 10 - 15 秒内完成,而不是串联的 20 - 30 秒。我会对任何可以实现这一点的答案感到满意,它不需要使用运行空间。
编辑:添加目标