PowerShell : error handling when using -asjob parameter

0

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

powershell
powershell-4.0
dsc
asked on Stack Overflow Jun 6, 2014 by Loïc MICHEL • edited Mar 22, 2021 by Jason Aller

1 Answer

1

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
answered on Stack Overflow Jun 6, 2014 by Raf

User contributions licensed under CC BY-SA 3.0