Search code examples
powershellpowershell-4.0dsc

PowerShell : error handling when using -asjob parameter


I can't figure out how to get the computernames that did not respond when using -asjob, could someone advise?

try{
    gwmi "Win32_OperatingSystem" -ComputerName $ordis -asjob  
    $resu=get-job | ? {$_.psjobtypename -eq "wmijob" } |wait-job |receive-job
}
catch{"error"}
$resu | select PSCOMPUTERNAME, @{name="lastboottime";expression={$_.converttodatetime($_.lastbootuptime)}} |sort lastboottime |ft
remove-job * -force

some hosts failed with the following error but I don't know which ones

Le serveur RPC n'est pas disponible. (Exception de HRESULT : 0x800706BA)
+ CategoryInfo : InvalidResult : (:) [], COMException
+ FullyQualifiedErrorId : JobStateFailed


Solution

  • Add another line to capture failed jobs after $resu | select.. statement, try/catch is not going to work as you are spawning wmi queries into separate run spaces.

    ....
    Get-Job -State Failed | Select-Object -ExpandProperty Location
    remove-job * -force