One of my favorite things to do with error handling in PowerShell is to structure the catch statement like this: $ErrorMessage = "Error occurred when attempting to do X on Y. This is a terminating error.`n$($Error[0])" Write-ErrorLog -LogMessage = $ErrorMessage return $ErrorMessage This will spit out a custom error message that you write yourself, and then on a new line spit out the text from the last error that happened (which is the one you just caught). It makes debugging waaaaay easier.
Jack I am quite surprised that you are not getting more hits as these videos are really good. Thanks
Thank you so much for creating this series. As a beginner , the series helped me to become fearless about powershell.
Happy to hear that! Thank you!
The quality of these videos man! I know for sure you'll make it big here
This series was very helpful .. thank you so much!
Nice Job! bite sized chunks, easy to digest. I will be subscribing
Thanks a lot, nice person!
This is amazing! Thank you for uploading
Great video!
One of my favorite things to do with error handling in PowerShell is to structure the catch statement like this:
$ErrorMessage = "Error occurred when attempting to do X on Y. This is a terminating error.`n$($Error[0])"
Write-ErrorLog -LogMessage = $ErrorMessage
return $ErrorMessage
This will spit out a custom error message that you write yourself, and then on a new line spit out the text from the last error that happened (which is the one you just caught).
It makes debugging waaaaay easier.
Thank u! Great Video
thanks for the video
$ErrorActionPreference = "Stop"
try {
$filePath = "C:\Scripts\10 - Error Handling"
$files = Get-ChildItem -Path $filePath
foreach ($file in $files) {
Write-Output $file.Name
}
Write-Output "This is after the loop"
} catch {
Write-Output "Caught an error"
} finally {
Write-Output "This always runs no matter what!"
}
nice
thanks