[Powershell] Get-ping

This is a function that gets the ping result for a certain client.
The function does only check if the client replies or not. if it replies, the current IP gets displayed.
Otherwise a “Offline” text will be displayed. This means the Hostname is offline or faulty.

The function takes a hostname as parameter and makes use of the System.Net.NetworkInformation to get the “ping” information.

   1: function Get-ping {
   2:     [CmdletBinding()] 
   3:     PARAM 
   4:     ( 
   5:         [Parameter(Position=1,
   6:                     Mandatory=$false,
   7:                     ValueFromPipeline = $true,
   8:                     Helpmessage="The computer name")] 
   9:         [string]
  10:         $computername="."
  11:     )
  12:     BEGIN
  13:     {
  14:         $result = @()
  15:         #set the erroractionpreference to SilentlyContinue. 
  16:         #(We're not concerned about offline machines)
  17:         $erroractionpreference = "SilentlyContinue"
  18:     }
  19:     PROCESS
  20:     {
  21:         $object = new-object Object
  22:         $object| add-member Noteproperty PC $computername.ToUpper()    
  23:         $ping = new-object System.Net.NetworkInformation.Ping
  24:         $Reply = $ping.send("$computername")
  25:         if ($Reply.status –eq “Success”) 
  26:         {
  27:                 $object| add-member Noteproperty Status `
  28:                         -Value $reply.Address.IPAddressToString
  29:         }
  30:         else 
  31:         {    
  32:             $object| add-member Noteproperty Status Offline
  33:         }
  34:         #Add the result to the array
  35:         $result += $object
  36:         $object = $null
  37:         
  38:     }
  39:     END
  40:     {
  41:         $result
  42:     }
  43: }

 Get-ping.ps1 (1.65 kb)

Attachment: Get-ping.zip
Published Thursday, February 11, 2010 3:20 AM by scallebaut

Comments

No Comments