talatham
6/12/2013 - 2:49 PM

Variety of methods for pinging machines.

Variety of methods for pinging machines.

# ============================================================
# Ping a list of machines v1.0
# Tom Latham 04/02/2010
#
# Note: only return specific errors on most common responses.
# ============================================================

# Function to open file dialog box and select a file
Function Get-FileName($initialDirectory)
{   
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")

    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialDirectory
    $OpenFileDialog.filter = "Text Files (*.txt)| *.txt" # Set the file types visible to dialog
    
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.filename
}

# Main Script

# Run Open File Dialog
$filename = Get-FileName -initialDirectory "c:"

# Get path of file to be used in storing results
$path = split-path $filename

# If results.txt exists, delete it
if (test-path $path\pingresults.txt)
{ remove-item $path\pingresults.txt }

# Extract content from filename object
$computernames = get-content $filename

echo "Running ping script."
echo "Results will be saved in $path\pingresults.txt"

# For each computer in list
foreach ($computer in $computernames)
{
    # Set query string to query ping status
    $strQuery = "select * from win32_pingstatus where address = '" + $computer + "'"
    
    # Get WMI object using query
    $wmi = gwmi -query $strquery
    
    switch($wmi.statuscode)
    {
        0 { "$computer `t Success" | out-file $path\pingresults.txt -append }
        11003 { "$computer `t Destination Host Unreachable" | out-file $path\pingresults.txt -append }
        11010 { "$computer `t Request Timed Out" | out-file $path\pingresults.txt -append }
        default { "$computer `t Not found" | out-file $path\pingresults.txt -append }
    }
}
'------------- USAGE --------------------

PingMachine("D01226632")

'------------- FUNCTION -----------------

Function PingMachine(sComputer)
' Ping a machine via WMI and return the status code
  
	Dim oStatus
	
	Dim oWMI : Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
	Dim oPings : Set oPings = oWMI.ExecQuery("Select * From Win32_PingStatus where Address = '" & sComputer & "'")
	For Each oStatus in oPings
		If (oStatus.StatusCode = 0) Then
			Ping = True
		Else
			Ping = False
			If IsNull(oStatus.StatusCode) Then
				wScript.Echo sComputer & " - not in domain."
			ElseIf (oStatus.StatusCode = 11010) Then
				wScript.Echo sComputer & " - is offline."
			End If
		End If
	Next
End Function
# ----------- FUNCTION ---------------------

# Ping machine
Function Ping-Machine($computer) { $wmi = gwmi -query "select * from win32_pingstatus where address = '$computer'"; return $wmi.StatusCode }

# ----------- USAGE ---------------------

$pingresults = Ping-Machine("D01226954")

# 0 is success
# 11003 is 'Destination Host Unreachable'
# 11010 is 'Request Timed Out'
Variety of methods for pinging machines.