2016-09-14 14 views
1

스크립트를 계속 진행하기 전에 현재 세션으로 설정하고 가져와야하는 두 개의 PSSession이 있습니다. 두 단계 모두 직렬로 실행할 때 약 20 - 30 초 동안 각각 약 10 - 15 초가 필요합니다.New-PSSession Parellel 실행

New-PSSession을 별도의 runspace에서 실행 한 다음 설정된 세션을 부모 프로세스로 가져올 수 있습니까?

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 초가 걸릴 것입니다. 나는 이것을 달성하는 어떤 대답이라도 행복 할 것이고, 그것은 runspaces를 사용할 필요가 없다.

편집 :

+1

봐 :

그래서 귀하의 경우, 당신은 아마 뭔가를 실행하여 당신이 원하는 것을 수행 할 수 있습니다. https://technet.microsoft.com/en-us/library/hh849717.aspx 여러 개의 ConnectionURI를 만들고 다른 개체에 할당 한 다음 실행을 계속할 수 있습니다. –

+2

또는 다른 당신은 Invoke-Command 스크립트 블록을 사용할 수 있습니다 –

+0

안녕하세요 @ ShankarShastri, 내가 완전히 당신의 제안을 이해하고 있는지 모르겠다. –

답변

2

신용 추가 목표는 올바른 방향으로 우리를 가리키는 위해 @ShankarShastri로 이동합니다. New-PSSession 커맨드 릿은 입력으로 URI 또는 ​​ComputerNames의 배열을 가져 오는 것을 지원합니다. 내가 대신의 URI에 대해 테스트하지만,이 살펴보고 서버를했다 : 새로운-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