【问题标题】:New-PSSession Parellel ExecutionNew-PSSession 并行执行
【发布时间】: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


【解决方案1】:

感谢@ShankarShastri 为我们指明了正确的方向。 New-PSSession commandlet 支持将一组 URI 或 ComputerNames 作为输入。我有要测试的服务器而不是 URI,但是看看这个:

$cred = Get-Credential DOMAIN\user

$servers = 
"server1.domain.company.com",
"server2.domain.company.com",
"server3.domain.company.com",
"server4.domain.company.com",
"server5.domain.company.com",
"server6.domain.company.com",
"server7.domain.company.com"

(Measure-Command {
    foreach($s in $servers) { $temp = New-PSSession -ComputerName $s -Authentication Negotiate -Credential $cred }
}).TotalSeconds

# 2.987739

(Measure-Command {
    $s1, $s2, $s3, $s4, $s5, $s6, $s7 = New-PSSession -ComputerName $servers -Authentication Negotiate -Credential $cred
}).TotalSeconds

# 0.5793281

这表明 New-PSSession 运行 7 次与运行 New-PSSession 一次并提供 7 个计算机名称。差异大约快 6 倍,这意味着连接是异步进行的。

所以在你的情况下,你可能可以通过运行类似的东西来完成你想要的:

$sessions1, $sessions2 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri ("https://$($service)/PowerShell/"),"https://outlook.office365.com/powershell-liveid/" -Credential $Credential -Authentication Basic -AllowRedirection -ErrorAction Stop

【讨论】:

    猜你喜欢
    • 2015-11-03
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    相关资源
    最近更新 更多