June 10 2009

Nifty Powershell script to ping multiple servers

This great Powershell script will ping all machines listed in a text file and put the live results into a Excel workbook:

$erroractionpreference = "SilentlyContinue"
$a = New-Object -comobject Excel.Application
$a.visible = $True
$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)
$c.Cells.Item(1,1) = "Machine Name"
$c.Cells.Item(1,2) = "Ping Status"
$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True
$d.EntireColumn.AutoFit($True)
$intRow = 2
$colComputers = get-content C:\temp\server_list.txt
foreach ($strComputer in $colComputers)
{
$c.Cells.Item($intRow, 1) = $strComputer.ToUpper()
# This is the key part
$ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.send($strComputer)
if ($Reply.status -eq "Success")
{
$c.Cells.Item($intRow, 2) = "Resolved & active"
}
elseif ($Reply.status -eq "TimedOut")
{
$c.Cells.Item($intRow, 2) = "Resolved host but timed out"
}
else
{
$c.Cells.Item($intRow, 2) = "Unable to resolve"
}
$Reply = ""
$intRow = $intRow + 1
}
$d.EntireColumn.AutoFit()

 



----------------------------------------------------------------------------
I use a maximum of one Google Ad per post to help offset some of my blog hosting costs.

----------------------------------------------------------------------------