Powershell try/catch/finally -
i wrote powershell script works great - however, i'd upgrade script , add error checking / handling - i've been stumped @ first hurdle seems. why won't following code work?
try { remove-item "c:\somenonexistentfolder\file.txt" -erroraction stop } catch [system.management.automation.itemnotfoundexception] { "item not found" } catch { "any other undefined errors" $error[0] } { "finished" }
the error caught in second catch block - can see output $error[0]
. catch in first block - missing? thanks
-erroraction stop
changing things you. try adding , see get:
catch [system.management.automation.actionpreferencestopexception] { "caught stopexecution exception" $error[0] }
Comments
Post a Comment