Reporting Errors to the End User

This topic contains 1 reply, has 2 voices, and was last updated by  Rob Simmers 2 years, 4 months ago.

  • Author
    Posts
  • #25360

    Robert Derickson
    Participant

    I am creating solution to batch account creations for one of my company's services. The users of this solution want to provide a CSV with account details and receive an email containing success or failure status for each account submitted, and a reason each failure occurred. Something friendlier than $_.Exception.Message.

    I currently have 15 different reasons account creation will fail. I have assigned each of these reasons a number, and use Switch to match reason codes to error messages. Example:

    $errCode = 0
    
    try {
        Something -ErrorAction Stop
    } catch {
        $errCode = 1
        Continue
    }
    try {
        SomethingElse -ErrorAction Stop
    } catch {
        $errCode = 2
        Continue
    }
    
    Switch ($errCode) {
        0 { $reason = "Success" }
        1 { $reason = "Account already exists" }
        2 { $reason = "ID number not valid" }
    }
    
    return $reason

    Is there a better way to handle this?

  • #25364

    Rob Simmers
    Participant

    Keep in mind you can only account for errors you know are going to happen, so you always need to capture what the "real" error is for you to troubleshoot and update your script with "known" issues. In your example, your try logic would try to do EACH step, so like Create Windows Account, Create Exchange, Create Lync would all be attempted even in step 1 failed, so make sure you nest your try's properly. I would basically append to the data I already have with the friendly status and admin status and then just use select at the end to send the user what they need to see. Here is the high-level logic I would try:

    $myCSV = @()
    $myCSV += New-Object -TypeName PSObject -Property @{FName="Joe";LName="Smith";EmpID="1254242421"}
    $myCSV += New-Object -TypeName PSObject -Property @{FName="Sally";LName="Johnson";EmpID="2464242511"}
    $myCSV += New-Object -TypeName PSObject -Property @{FName="Frank";LName="Kelly";EmpID="35261241234"}
    
    $results = foreach($user in $myCSV) {
        try{
            Something -ErrorAction Stop
            try {
                #Another try ONLY if step 1 completes, make sure you are stopping the process if
                # Step 1 completes successful...
            }
            catch {
    
            }
    
        }
        catch{
            $adminError = $_.Exception.Message
            switch -WildCard ($adminError) {
                "*already exists*"{
                    $friendlyError = "Account already exists for {0}" -f $user.EmpID
                }
                "*another known error*"{
                    $friendlyError = "Something I know about happened {0}" -f $user.EmpID
                }
                default {
                    #You could create a flag here too to email you or search for the "Something occurred.." in a where below to let you know something bad happened that wasn't accounted for
                    $friendlyError = "Something occured I didn't code for. {0}" -f $adminError
                }
            }
        }
    
        $user | Select FName, LName, EmpID, @{Label="Status";Expression={$friendlyError}}, @{Label="AdminStatus";Expression={$adminError}}
    }
    
    #Send HTML email with info or attach the CSV with the "friendly" messages
    $user | Select Select FName, LName, EmpID, Status | Export-CSV -Path EmailBackToUser.csv
    

    Edit: Saw a couple of code mistakes. Also, this is just an example and won't actually execute, but give you an idea of the flow...

You must be logged in to reply to this topic.