2014-04-30 3 views
0

작업을 시작하는 데 어려움이 있으며 문제가 무엇인지 알아내는 데 어려움이 있습니다. 대부분의 작업이 전부가 아니라면 대부분 완료되지 않습니다. 아래 코드는 작업으로 시작하지 않을 때 올바르게 작동했습니다.Powershell Start-Job 작업이 완료되지 않았습니다.

$timer = [System.Diagnostics.Stopwatch]::StartNew() 
$allServers = Import-Csv "C:\temp\input.csv" 
$password = GC "D:\Stored Credentials\PW" | ConvertTo-SecureString 


$allServers | % { 
    Start-Job -ArgumentList $_.ComputerName,$_.Domain -ScriptBlock { 
     param($sv,$dm) 
     $out = @() 

     #Determine credential to use and create password 
     $password = GC "D:\Stored Credentials\PW" | ConvertTo-SecureString 
     switch ($dm) { 
      USA {$user = GC "D:\Stored Credentials\MIG"} 
      DEVSUB {$user = GC "D:\Stored Credentials\DEVSUB"} 
      default {$cred = ""} 
      } 
     $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user,$password 

     #Query total cpus 
     $cpu = ((GWMI win32_processor -ComputerName $sv -Credential $cred).NumberOfLogicalProcessors | Measure-Object).Count 

     $outData = New-Object PSObject 
     $outData | Add-Member -Type NoteProperty -Name "ComputerName" -Value $sv 
     $outData | Add-Member -Type NoteProperty -Name "#CPU" -Value $cpu 

     $out += $outData 
     return $out 
     } 
    } 

while (((Get-Job).State -contains "Running") -and $timer.Elapsed.TotalSeconds -lt 60) { 
    Start-Sleep -Seconds 10 
    Write-Host "Waiting for all jobs to complete" 
    } 
Get-Job | Receive-Job | Select-Object -Property * -ExcludeProperty RunspaceId | Out-GridView 

답변

1

out += $outData; return $out은 무엇인가요? 이 코드가 루프에서 실행되고 있다고 생각되지만 그렇지 않습니다. 외부 foreach-object가 복수 independent 작업을 시작합니다. 각각은 하나의 $outData을 만듭니다. 당신이 재산 #CPU 이름을 다음이 때문에 액세스 할 수있는 번거 로움이 경우

$outData = New-Object PSObject 
$outData | Add-Member -Type NoteProperty -Name "ComputerName" -Value $sv 
$outData | Add-Member -Type NoteProperty -Name "#CPU" -Value $cpu 
$outData 

내가 BTW

[pscustomobject]@{ComputerName = $sv; CpuCount = $cpu} 

(V3에) 조금 더 단순화 것이다 : 당신은 단지 코드의 마지막 비트를 단순화 할 수 당신이이 건물의 이름 예를 들어, 인용 할 필요가 :

$jobs = $allServers | % { 
    Start-Job -ArgumentList $_.ComputerName,$_.Domain -ScriptBlock { ... } 
} 
Wait-Job $jobs -Timeout 60 
Receive-Job $jobs | Select -Property * -ExcludeProperty RunspaceId | Out-GridView 
: 또한 $obj.'#CPU'

을 당신은이에 대기 루프를 단순화 할 수 있습니다

비록

관련 문제