Wednesday, July 6, 2011

PowerShell: Quick and Simple Network Monitor

Helped someone with a quick network monitoring PowerShell script. They are loading the list of systems from a text file on the locale system.

#Variable for System Count
$x = 0

#Email Message Body
$emsg = "<p>The following computer(s) failed Ping:</p>"

#Report Date
$rptDate = Get-Date

#Load Array of Computer Systems
$networkSystems = Get-Content "C:\data\network_systems.txt"

#Ping Each System in Array
foreach($ns in $networkSystems)
{
#If Ping Fails then Add to Message
if (!(test-connection -computername $ns -quiet))
{
$x = $x + 1
$emsg = $emsg + $ns + "<br />"
}

}

if($x -gt 0)
{
#Settings for Email Message
$messageParameters = @{
Subject = "Failed Ping Report for " + $rptDate
Body = $emsg
From = "report@mycollege.edu"
To = "admin@mycollege.edu"
SmtpServer = "smtp.mycollege.edu"
}
#Send Email Message
Send-MailMessage @messageParameters –BodyAsHtml
}

No comments: