Powershell WMIObject Error Handling -
i have code connects remote servers. need write text-file if connection server cannot made. i've done code:
try { $w3wpresult = (get-wmiobject win32_process -filter "name 'w3wp%'" -computername $server | select name, @{l= "private memory (gb)"; e={$_.privatepagecount / 1gb}}) $vmresult = (get-wmiobject win32_process -filter "name 'w3wp%'" -computername $server | select name, @{l= "virtual memory (gb)"; e={$_.virtualsize / 1gb}}) $vmmemory += $server + " @ " + $time + ": " + (($vmresult|%{"$_"})-join',') + "`r`n" $w3wpmemory += $server + " @ " + $time + ":" + (($w3wpresult|%{"$_"})-join',') + "`r`n" } catch [system.runtime.interopservices.comexception] { if($_.exception.errorcode -eq 0x800706ba) { $errormessage = "unable connect server" } $vmmemory += $server + " @ " + $time + ": " + $errormessage $w3wpmemory += $server + " @ " + $time + ": " + $errormessage }
the $errormessage
variable not holding error message.
any appreciated. thanks.
just include error action preference wmi commands like:
(get-wmiobject win32_process -filter "name 'w3wp%'" -computername $server -erroraction stop
this should catch non-terminating errors . include generic catch block catch exceptions not mentioned.
try { #wmi commands } catch [system.runtime.interopservices.comexception] { #specific error caught } catch { #generic error caught }
Comments
Post a Comment