2016-09-14 2 views
1

スクリプトを続行するには、2つのPSSessionを確立して現在のセッションにインポートする必要があります。両方のステップでは、連続して実行する場合、合計20〜30秒間、それぞれ約10〜15秒かかる。New-PSSession Parellel Execution

別のランスペースで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を並行して使用すると、それは20〜30秒ではなく10〜15秒で完了します。私はこれを達成するすべての答えに満足しているでしょう、それは実行スペースを使用する必要はありません。

EDIT:

+1

ルック:

は、だからあなたの場合、あなたはおそらくのようなものを実行して、あなたが望むものを達成することができます。 https://technet.microsoft.com/en-us/library/hh849717.aspx 複数のConnectionURIを作成して他のオブジェクトに割り当てることができ、実行を続行できます。 –

+2

それ以外の場合は、Invoke-Commandスクリプトブロックを使用することができます –

+0

こんにちは@ShankarShastri、私はあなたの提案を完全に理解しているかどうかはわかりませんが、 –

答えて

2

クレジットを追加しました目標は正しい方向に私たちを指しているため@ShankarShastriに行きます。 New-PSSessionコマンドレットは、URIまたはComputerNamesの配列を入力として受け入れることをサポートしています。私の代わりにURIのに対してテストが、これを見てみましょうするためにサーバを持っています。New-PSSessionコマンドの実行に一度新しい-PSSessionコマンドを実行し、7 ComputerNamesを供給対7回を示し

$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 

。差は約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 
関連する問題