Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

If statement in a Try block

$
0
0

I have a Ping function that I am using to ping machines with in a bigger script.  One thing that I noticed is that sometime I get a failure and it reports the machine as "timed out"

I would like to run another ping within this function to try it again to ensure that the machine is actually timing out.  Is there a way to put an If else statement in a Try block?

 Here is the function

  function Ping([string]$hostname, [int]$timeout = 200) {
      $ping = new-object System.Net.NetworkInformation.Ping #creates a ping object
       try { $result = $ping.send($hostname, $timeout).Status.ToString() }
       catch { $result = "Failure" }
    return $result
 }


Viewing all articles
Browse latest Browse all 6937

Trending Articles