我在启动工作时遇到了一些困难,而且我很难找出问题所在。大多数(如果不是全部)工作都没有完成。下面的代码在没有作为工作开始时正常工作。
$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