Created
March 7, 2017 18:48
-
-
Save gpduck/cd0449ddbe83b2168e96758e185b4110 to your computer and use it in GitHub Desktop.
PowerShell Errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Error.Clear() | |
Try { | |
Get-Process fsdks -ErrorAction Stop | |
} Catch { | |
Write-Error "Couldn't find the process" | |
} | |
#Prints out the error "Couldn't find the process" | |
#This contains 2 objects, the "Couldn't find the process" error, and the original error from Get-Process | |
$Error.Count | |
$Error | |
#Reset to go again | |
$Error.Clear() | |
Try { | |
Get-Process fsdks -ErrorAction Stop | |
} Catch { | |
#Trying to wrap the original error with more relevant detail | |
Write-Error "Couldn't find the process" -Exception $_.Exception | |
} | |
#Prints out the error "Couldn't find the process" | |
#Now we only have the error from Get-Process. Even though the additional detail error was printed to the screen, it does not get saved in $Error | |
$Error.Count | |
$Error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment