Acerbity
Acerbity

Reputation: 527

Powershell Start-Job jobs not completing

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

Upvotes: 0

Views: 709

Answers (1)

Keith Hill
Keith Hill

Reputation: 201662

What's with the out += $outData; return $out? It appears you think this code is executing in a loop but it isn't. The outer foreach-object starts up multiple independent jobs. Each one creates a single $outData. You could simplify the last bit of code to just:

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

I would simplify a bit further (on V3)

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

BTW if you name the property #CPU then it is a hassle to access because you have to quote the property name e.g.: $obj.'#CPU'

Also you can simplify your wait loop to just this:

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

Although

Upvotes: 1

Related Questions