« Deployment4 Beta 3 Observations | Main | Ubuntu 7.10 Countdown »
Error checking in PowerShell
By Adam Bell | October 2, 2007
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 ….
- Utilising CMD behaviour in PowerShell
- Power Lines
- Static Members and PowerShell obscurity
- Converting a GUID string to octet string
Topics: PowerShell | 1 Comment »
October 3rd, 2008 at 17:52
Hi,
I added this in my code, after and $user.setinfo() command, but the error is not caught.
Can you help?
Lex
…rest of script….
$GetUser.setinfo()
if (!$?)
{
Write-Host “The is an Error in this code”
}
Else
{
Write-Host “There is NO Error in this code”
}