none
PowerShell: Exceptions [try/catch/throw]; How to use them with conditionals?

    Question

  • In my scripting experience, I've never used try/catch/throw so I'm trying to wrap my head around the concepts.  But one aspect I'm missing is how to use them for conditional code flow?

    For example, I just read a really interesting article on multi-threading with PowerShell and wanted to test it out on a larger subset so to get ready I:

    -  Created a text file with approx. 100 servers in it as my test case
    -  Created the process I wanted to multi-thread (simple ping using System.Net.Networkinformation.Ping)
    -  Ran the script.

    I noticed that on a number of the hosts when the output was given, I had socket exception errors.  My assumption ended up being correct that these server objects (that i'd pulled out of AD), didn't have DNS records.  So I started reading blog articles on using t/c/th to try and determine a way to provide back an error specificially stating the problem if there was a problem and eded up with a try/catch using a DNS lookup and ended up with the following (very basic) script:

    Param($ComputerName = "LocalHost")
    try {
     $net = [System.Net.Dns]::GetHostAddresses($ComputerName)
    } catch [Exception] {
     write-host $_.Exception.GetType().FullName; 
      write-host $_.Exception.Message; 
    }
    $ping = new-object system.net.networkinformation.ping
    $ping.send($ComputerName)

    But my question is, how can I use t/c/th to say something like:

    Try nslookup, if good, run ping; if not, return exception message.

    ...I just am not seeing how to evaluate the information in order to use it to direct the flow of code... any help is greatly appreciated.

    Thursday, June 23, 2011 4:39 PM

Answers

  • Param($ComputerName = "LocalHost")
    try {
     $net = [System.Net.Dns]::GetHostAddresses($ComputerName)
    } catch [Exception] {
       return $_.Exception.Message
    }
    $ping = new-object system.net.networkinformation.ping
    $ping.send($ComputerName)

     

    The "return" keywork will exit the function or script block after returning the exception message, and the ping will not be run.  If the lookup is successful, the catch block will not be run, and fuction or scriptblock will continue with running the ping


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by thepip3r Thursday, June 23, 2011 5:13 PM
    Thursday, June 23, 2011 4:52 PM
    Moderator

All replies

  • Param($ComputerName = "LocalHost")
    try {
     $net = [System.Net.Dns]::GetHostAddresses($ComputerName)
    } catch [Exception] {
       return $_.Exception.Message
    }
    $ping = new-object system.net.networkinformation.ping
    $ping.send($ComputerName)

     

    The "return" keywork will exit the function or script block after returning the exception message, and the ping will not be run.  If the lookup is successful, the catch block will not be run, and fuction or scriptblock will continue with running the ping


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by thepip3r Thursday, June 23, 2011 5:13 PM
    Thursday, June 23, 2011 4:52 PM
    Moderator
  • Param($ComputerName = "LocalHost")
    try{
       $net=[System.Net.Dns]::GetHostAddresses($ComputerName)
    }
    catch [Exception] {
       #write-host $_.Exception.GetType().FullName; 
       write-host $_.Exception.Message;
       return
    }
    try{
       $ping = new-object system.net.networkinformation.ping
       $ping.send($ComputerName)
    }
    catch{
       #write-host $_.Exception.GetType().FullName; 
       write-host $_.Exception.Message; 
    }
    


    jv
    • Proposed as answer by jrv Thursday, June 23, 2011 5:32 PM
    Thursday, June 23, 2011 4:57 PM
  • Alternate approach:

    Param($ComputerName = "LocalHost")
    try{
       $net=[System.Net.Dns]::GetHostAddresses($ComputerName)
       try{
         $ping = new-object system.net.networkinformation.ping
         $ping.send($ComputerName)
       }
       catch{
         #write-host $_.Exception.GetType().FullName; 
         write-host $_.Exception.Message; 
       }
    }
    catch [Exception] {
       #write-host $_.Exception.GetType().FullName; 
       write-host $_.Exception.Message;
    }
    
    

     


    jv
    Thursday, June 23, 2011 4:59 PM
  • mj - is there a book you'd recommend for Powershell that you wouldn't mind sharing?

    Thursday, June 23, 2011 5:14 PM
  • I'd recommend Bruce  Payette's Powershell in Action - Second Edition. 

    http://www.manning.com/payette2/


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Thursday, June 23, 2011 5:19 PM
    Moderator
  • I ran across your blog post here.  So I was wondering, if I run write-host $_.exception.message, it actually writes on the screen, write-host $_.exception.message.  Is there something I'm doing wrong to be getting the command outputted to the screen instead of the actual results?  I even tried:

    {

    $variablex = catch [Exception]

    write-host $_.exception.message

    }


    JCtech1123, Cheers

    Monday, February 27, 2012 2:15 AM
  • Hi,

    This question is already marked answered - please start a new question (you can reference this thread for background if needed); thanks.

    Bill

    Monday, February 27, 2012 2:30 AM
    Moderator
  • I did.  Hopefully someone sees it.  Thx.

    JCtech1123, Cheers

    Wednesday, February 29, 2012 3:07 AM