Link to home
Start Free TrialLog in
Avatar of Jason Crawford
Jason CrawfordFlag for United States of America

asked on

PowerShell error handling best practices

Question on PowerShell error handling.  Which method is 'better' and why?

Using Try/Catch to handle any errors for Do-Something

try {
    Do-Something -ErrorAction 'Stop'
}
catch {
    $errormessage = $_.Exception.Message
    Write-Verbose "An error occurred: $errormessage"
}

Open in new window

Using an If statement to handle errors for Do-Something

if (Do-Something) {
    Do-SomethingElse
}
else {
    $errormessage = $_.Exception.Message
    Write-Verbose "An error occurred: $errormessage"
}

Open in new window

SOLUTION
Avatar of Bell TechLogix
Bell TechLogix
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jason Crawford

ASKER

@Qlemo here's what prompted the question - I had these two sections of my script sitting right next to each other and I honestly couldn't nail down why I used try/catch for Disable-MailContact whereas I used an if statement for Get-MailContact:

try {
    Disable-MailContact $user -Confirm:$false -ErrorAction 'stop'
}
catch {
    $errormessage = $_.Exception.Message
    Write-Verbose "An error occurred: $errormessage"
}

if (!(Get-MailContact $user)) {
    if (Enable-MailUser $aduser.SamAccountName -ExternalEmailAddress $contact.ExternalEmailAddress) {
        Set-MailUser $user -EmailAddresses @{add=$contact.emailaddresses}
        Set-MailUser $user -EmailAddresses @{add=$legdn}
    }
}

Open in new window

Is there a better way to do this?
what is the result you are getting, and what is the result you are expecting?
The specific results don't matter I'm just asking from a best practices point of view for my own education.
The best answer is try the (Try,Catch) but if it doesn't work in your scenario then move to the next option, and that's checking the results, and if that doesn't work, then pull the source a second time and verify you have changed what you wanted.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's an interesting thought though.  For instance if I wanted to take action when a MailContact isn't found I could catch the specific error generated for object not found:

try {
    Get-MailContact $_
}

catch [ManagementObjectNotFoundException] {
    Enable-MailUser $aduser.samaccountname -ExternalEmailAddress $_
    Set-MailUser $_ -EmailAddresses @{add=$contact.emailaddresses}
}

catch {
    Write-Verbose "An error occurred: $($error[0].exception.message)"
}

Open in new window

Thanks for helping me talk this through.