RSS All Posts

RSS PowerShell Posts

Tags

2142 Active Directory Administrativia Announcements Battlefield Blogging Cricket Deployment Deployment4 Get-PSUGUK Group Policy HowTo Linux MDT MDT 2010 Microsoft Deployment Toolkit MSDN Music Permissions Personal PowerGui Power Lines PowerShell PowerShell Groups PowerShell Support PowerShell Tools PowerShell V2 Presentations PSUGAU Quick Tips Scripting SDDL Security Tech Talk Ubuntu User Groups Virtualisation VMware Infrastructure Client WAIK Weekly Poll Windows 7 Windows Automation Installation Kit Windows Server 2003 Windows Server 2008 XML

Archives

Meta


« | Main | »

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 ….

Topics: PowerShell | 1 Comment »

One Response to “Error checking in PowerShell”

  1. Lex Mangin Says:
    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”
    }

Comments