I recently thought I ought to lift my scripting game and introduce some error checking into my PowerShell scripts.
After a quick google I found this excellent article from the Scripting Guys.
In essence, we set:
$error.clear()
$erroractionpreference = “SilentlyContinue”
And then use, the following statement to test whether the last command was successful:
if (!$?)
{
“An error has occurred.”
}
If you just want to know whether an error occurred within the script, you can test:
if ($error[0])
{
“An error has occurred”
}
The article is good and well worth a read. Watch this space for some examples shortly ….