I am having some difficulty launching jobs and I am having trouble finding out what the issue is. Most if not all of the jobs just don't complete. Code below was working correctly when not started as a 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