ping.ps1

This is an example on how to ping a hostname or ipaddress with powershell. It makes use of the WMI Win32_Pingstatus to ping the address.

#***************************************************************************
#** Script: ping.ps1
#** Version: 1.0
#** Created: 12/19/2010 11:09AM
#** Author: Adriaan Westra
#** Website: http://www.westphil.nl
#**
#** Purpose / Comments:
#** Ping a hostname
#**
#**
#** Changelog :
#** 12/19/2010 11:09AM : Initial version
#**
#***************************************************************************
#***************************************************************************
#** Function: ping
#** Version: 1.0
#** Created: 12/19/2010 11:09AM
#** Author: Adriaan Westra
#** Website: http://www.westphil.nl
#**
#** Purpose / Comments:
#** Ping a hostname/ipaddress
#**
#**
#** Arguments :
#** Hostname or Ipaddress
#**
#**
#**
#** Returns :
#** $result.ReturnCode (see win32_pingstatus) should be 0
#** $result.ReturnAddress (see win32_pingstatus) IP4 address
#** $result.Responsetime (see win32_pingstatus)
#**
#**
#** Changelog :
#** 12/19/2010 10:09AM : Initial version
#**
#***************************************************************************
function Ping {
param(
$Ipaddress
)
$test = Get-WmiObject -Class Win32_PingStatus -Filter “Address=’$($ipaddress)'” -ComputerName .
$Result = New-object system.object
$result | add-member -type Noteproperty -name Description -value “Ping Result”
$result | add-member -type Noteproperty -name ReturnCode -value $test.StatusCode
$result | add-member -type Noteproperty -name ReturnAddress -value $test.IPV4Address.ipaddresstostring
$result | add-member -type Noteproperty -name ResponseTime -value $test.ResponseTime
return $result
} # End Function
#***************************************************************************
#** Start of script
#***************************************************************************
#** Clear the screen
Clear-Host
#** Initialize the variables
$hostname = “asussrv01”
$** Call the ping function
$result = ping -ipaddress $hostname
#** If returncode = 0, result OK
If ($result.returncode -eq 0 ) {
Write-host “The IP address is : ” $result.ReturnAddress
Write-host “The response time is : ” $result.ResponseTime }
Else {
Write-host $hostname ” is not responding.” }

Geef een reactie