July 2018
Beginner
552 pages
13h 18m
English
Terminating errors can be generated on demand, or by setting the ErrorActionPreference for the entire session to Stop. Terminating errors need to be handled in your code; otherwise, they will also terminate your script. Hence, the name. The usual error handling is done with a try/catch block. We try to execute a cmdlet, catch any exception, and, finally, do a cleanup task that is always executed, even if no exception has been thrown:
try{ $items = Get-Item -Path C:\Does\Not\Exist, C:\Windows, $env:APPDATA -ErrorAction Stop}catch [System.Management.Automation.ItemNotFoundException]{ # Specific catch block for the exception type # PSItem contains the error record, and TargetObject may contain the actual object raising the ...Read now
Unlock full access