Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

ping-wmi.ps1 - powershell script by ActiveXperts Software

ping-wmi.ps1 pings a remote host through WMI.

Use ping-wmi.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select ping-wmi.ps1. Configure the required parameter, or press 'Load a working sample'.

In ActiveXperts Network Monitor, Administrators can use three different scripting languages: Powershell, VBScript and SSH.


ping-wmi.ps1 script code

#################################################################################
# ActiveXperts Network Monitor PowerShell script, © ActiveXperts Software B.V.
# For more information about ActiveXperts Network Monitor, visit the ActiveXperts 
# Network Monitor web site at http://www.activexperts.com
#################################################################################
# Script
#     Ping-wmi.ps1
# Description:
#     Ping a remote host.
# Declare Parameters:
#     1) strHost (string) - Hostname or IP address of the computer you want to monitor
# Usage:
#      .\Ping-wmi.ps1 '<Hostname | IP>'
# Sample:
#      .\Ping-wmi.ps1 'www.activexperts.com'
#################################################################################

# -- Declare Parameters
param( [string]$strHost = '' )

cls
$Error.Clear()

# Check parameters input
if( $strHost -eq '' )
{
  $res = 'UNCERTAIN: Invalid number of parameters - Usage: .\ping-wmi.ps1 "<Hostname | IP>"'
  echo $res
  exit
}

#################################################################################
# // --- Main script ---
#################################################################################

$objPing = Gwmi Win32_PingStatus -Filter 'Address ="$strHost"' | Select-Object StatusCode 

if( $objPing -eq $null )
{
  $res = 'UNCERTAIN: Failed to execute Ping'
  echo $res
  exit
}

# -- Check if the ping was successful or not
if( $objPing.StatusCode -ne 0 )
{ 
  $res = 'ERROR: Destination host (' + $strHost + ') unreachble'
}
else
{
  $res = 'SUCCESS: Ping to (' + $strHost + ') was successfull'
}

# -- Print script result
echo $res	
exit


#################################################################################
# // --- Catch script exceptions ---
#################################################################################

trap [Exception]
{
  $res = "UNCERTAIN: " + $_.Exception.Message
  echo $res
  exit
}