Script, piping and error handling

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Old Cole Old Cole 2 years, 6 months ago.

  • Author
    Posts
  • #15446
    Profile photo of Old Cole
    Old Cole
    Participant

    Okay confesioon time, most of my scripts to date have been aimed at my use and don't include error controls. I'm trying to stpe things up a little and write better scripts.

    So without error handling I have something like:

    Get-ADUser -LDAPFilter $LDAPUserFilter -ResultSetSize $null -Properties $ListofUserADProperties | %{
    $GN = $_.GivenName
    $SN = $_.Surname
    $CN = $_.cn
    $Mail = $_.mail
    $Addresses = $_.proxyAddresses
    $DN = $_.DistinguishedName
    }

    In the past I'm sure I read that best practice is to process along the pipeline rather than return all results into an object?

    Using Try / Catch is this still true?

    The following doesn't appear to be acceptable as the curly bracket closes the loop rather than the catch:
    Try {
    Get-ADUser -LDAPFilter $LDAPUserFilter -ResultSetSize $null -Properties $ListofUserADProperties | %{
    $GN = $_.GivenName
    $SN = $_.Surname
    $CN = $_.cn
    $Mail = $_.mail
    $Addresses = $_.proxyAddresses
    $DN = $_.DistinguishedName
    }
    }

    Thanks

    Paul

  • #15452
    Profile photo of Peter Jurgens
    Peter Jurgens
    Participant

    Firstly it seems you haven't included a catch statement in your script. The syntax for try/catch/finally is like this:

    Try{}Catch{}Finally{}
    

    You're require to have at least the try and catch blocks.
    In the try block you will put your code that you want to catch errors for, and in the catch block you would enter the alternate code or output should the code in the try block fail with a terminating error.

    The terminating error part is also important. If the script in your try block does not fail with a terminating error, try/catch will not "catch" the error and run the script in the catch block. For most cmdlefs you can add the -ErrorAction Stop parameter to the cmdlet in the try block.

  • #15460
    Profile photo of Mike F Robbins
    Mike F Robbins
    Participant

    Here's an example of using a Try, Catch, Finally block to handle errors. As the previous person said. Try, Catch, Finally only handles terminating errors so you'll need to use -ErrorAction Stop to turn non-terminating errors into terminating ones.

    
    try {
        1/0
    }
    catch [DivideByZeroException] {
        Write-Warning -Message "Divide by zero exception"
    }
    catch [System.Net.WebException],[System.Exception] {
        Write-Warning -Message "Other exception"
    }
    finally {
        Write-Warning -Message "cleaning up ..."
    }
    

    Try is mandatory and either Catch or Finally is required, although you'll almost always see a Catch block and Finally omitted. As a general rule, don't wrap an entire script or function in Try, Catch, Finally, just use it where you think errors might occur. See the about_try_catch_finally help topic in PowerShell for more information. I also have some examples of using error handling in PowerShell scripts and functions on my blog site. Here's one that has a couple of simple examples of using error handling.

  • #15467
    Profile photo of Old Cole
    Old Cole
    Participant

    Thanks both for the reply. I think I excluded too much from my snippet!

    Is this best?

    
    try {
        Get-ADUser -LDAPFilter $LDAPUserFilter -ResultSetSize $null -Properties $ListofUserADProperties | ForEach-Object {
            $GN = $_.GivenName 
            write-host $GN	
        }	
    } catch {
        #Bad outcome
    }

    or, is this better

    
    try {
        $ADUsers = Get-ADUser -LDAPFilter $LDAPUserFilter -ResultSetSize $null -Properties $ListofUserADProperties
    }
    Catch {
        #Something bad happened
    }
    
    ForEach($User in $ADUsers){
        write-host $User.GivenName    
    }

You must be logged in to reply to this topic.