【发布时间】:2016-09-14 13:35:00
【问题描述】:
在我们的脚本可以继续之前,我们需要建立两个 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 秒。我会对任何可以实现这一点的答案感到满意,它不需要使用运行空间。
编辑:添加目标
【问题讨论】:
-
查看本网站。 technet.microsoft.com/en-us/library/hh849717.aspx您可以创建多个ConnectionURI并将其分配给其他对象,然后继续执行。
-
否则您可以使用 Invoke-Command 脚本块
-
嗨@ShankarShastri,我不确定我是否完全理解你的建议,你介意提供一个例子吗?
标签: powershell powershell-remoting runspace