locked
Try Catch works but finally not working RRS feed

  • Question

  • I use try and catch all the time.  I have never needed finally but thought for sure it would be as simple as adding it after the catch. I was wrong.

    Can anyone tell me what i am doing wrong.  

    Thanks!


    try {$OptionMGSFTBootstrapServer  =  (Get-DhcpServerv4OptionValue -ComputerName "$server" -ScopeId $scope -OptionId 188 -ErrorAction Stop).value  } catch {$OptionMGSFTBootstrapServer   = (Get-DhcpServerv4OptionValue -ComputerName "$server" -OptionId 188 ).value } finally {$OptionMGSFTBootstrapServer = "Failed"}
    try {$Optioniexdhcpoption  =  (Get-DhcpServerv4OptionValue -ComputerName "$server" -ScopeId $scope -OptionId 195 -ErrorAction Stop).value  } catch {$Optioniexdhcpoption   = (Get-DhcpServerv4OptionValue -ComputerName "$server" -OptionId 195 ).value } finally {$Optioniexdhcpoption = "Failed"}

    PS this is the error

    Get-DhcpServerv4OptionValue : Failed to get option value of 195 on DHCP server ms01acomm02.schools.gcps1.gwin.
    At C:\Users\9201401145\Google Drive\Scripts\PowerShell\GET ALL DHCP SERVER SCOPE INFO.ps1:58 char:175
    + ... option   = (Get-DhcpServerv4OptionValue -ComputerName "$server" -Opti ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (195:root/Microsoft/...erv4OptionValue) [Get-DhcpServerv4OptionValue], CimException
        + FullyQualifiedErrorId : DHCP 20010,Get-DhcpServerv4OptionValue
     
    


    Lishron


    • Edited by Lishron Friday, June 26, 2015 5:40 PM
    Friday, June 26, 2015 5:38 PM

Answers

  • OK I believe i have the answer.

    First I had to figure out what type of error was being generated. 

    I placed this under the 2 lines that was causing the errors.

    $Error[0] | fl * -Force

    It allowed me to see the block of text I had to put in after the catch.  I did not know I could do multiple catch statements based on the error.

    I modified the 2 lines in my script (see below) and it is working.

    I THINK I figured out my own issue but will admit I am not 100% for sure. I will leave this open for a little wile and hope someone can confirm this is a acceptable way to deal with the issue i am having.

    Thanks

    Try {$OptionMGSFTBootstrapServer  =  (Get-DhcpServerv4OptionValue -ComputerName "$server" -ScopeId $scope -OptionId 188 -ErrorAction Stop ).value  } catch [System.Management.Automation.InvocationInfo] {$OptionMGSFTBootstrapServer   = (Get-DhcpServerv4OptionValue -ComputerName "$server" -OptionId 188 ).value } catch {$OptionMGSFTBootstrapServer = "Failed"}
    
    $Error[0] | fl * -Force
    try {$Optioniexdhcpoption  =  (Get-DhcpServerv4OptionValue -ComputerName "$server" -ScopeId $scope -OptionId 195 -ErrorAction Stop).value  } catch [System.Management.Automation.InvocationInfo]  {$Optioniexdhcpoption   = (Get-DhcpServerv4OptionValue -ComputerName "$server" -OptionId 195 ).value } catch  {$Optioniexdhcpoption = "Failed"}
    
    $Error[0] | fl * -Force


    Lishron


    • Edited by Lishron Friday, June 26, 2015 6:18 PM
    • Marked as answer by Lishron Friday, June 26, 2015 7:25 PM
    Friday, June 26, 2015 6:17 PM

All replies

  • OK I believe i have the answer.

    First I had to figure out what type of error was being generated. 

    I placed this under the 2 lines that was causing the errors.

    $Error[0] | fl * -Force

    It allowed me to see the block of text I had to put in after the catch.  I did not know I could do multiple catch statements based on the error.

    I modified the 2 lines in my script (see below) and it is working.

    I THINK I figured out my own issue but will admit I am not 100% for sure. I will leave this open for a little wile and hope someone can confirm this is a acceptable way to deal with the issue i am having.

    Thanks

    Try {$OptionMGSFTBootstrapServer  =  (Get-DhcpServerv4OptionValue -ComputerName "$server" -ScopeId $scope -OptionId 188 -ErrorAction Stop ).value  } catch [System.Management.Automation.InvocationInfo] {$OptionMGSFTBootstrapServer   = (Get-DhcpServerv4OptionValue -ComputerName "$server" -OptionId 188 ).value } catch {$OptionMGSFTBootstrapServer = "Failed"}
    
    $Error[0] | fl * -Force
    try {$Optioniexdhcpoption  =  (Get-DhcpServerv4OptionValue -ComputerName "$server" -ScopeId $scope -OptionId 195 -ErrorAction Stop).value  } catch [System.Management.Automation.InvocationInfo]  {$Optioniexdhcpoption   = (Get-DhcpServerv4OptionValue -ComputerName "$server" -OptionId 195 ).value } catch  {$Optioniexdhcpoption = "Failed"}
    
    $Error[0] | fl * -Force


    Lishron


    • Edited by Lishron Friday, June 26, 2015 6:18 PM
    • Marked as answer by Lishron Friday, June 26, 2015 7:25 PM
    Friday, June 26, 2015 6:17 PM
  • Why do you think you need to include 'finally?' Finally doesn't help you mitigate an error; it's just there to do something, regardless of if there's an error or not. Consider the following two commands. In the first one, it can locate a user in Active Directory, and in the second, it cannot. In both examples finally still happens.

    try {Get-ADUser -Identity bsmith | Select-Object SamAccountName} catch {echo 'failure'} finally {echo 'finally'}
    
    SamAccountName
    --------------
    bsmith
    finally
    
    try {Get-ADUser -Identity notarealuser | Select-Object SamAccountName} catch {echo 'failure'} finally {echo 'finally'}
    failure
    finally
    What you probably need to do to ensure you error says "hidden," is make use of the -ErrorAction parameter and Stop value. Start working that into your Get-DhcpServer... cmdlets. In fact, I used it in a recently, published function: https://gallery.technet.microsoft.com/Find-DNS-Servers-Being-640978d1 that used some of these same Dhcp cmdlets.

    Edit: Added more info.


    Friday, June 26, 2015 6:22 PM
  • Thanks very much.  I think i figured out my own answer right above yours.


    Lishron

    Friday, June 26, 2015 6:36 PM