Thursday 20 June 2013

Powershell Script to Ping Machines or Servers and Email Out the Results

If you need to run a script to determine whether a particular server or set of servers are awake and repsonding to pings, then email out the results you can create a powershell script using the following code:

####Define the servers that need pinging####

$ServerName = "MS-Robot1","MS-Robot2","MS-DC2"

####Start the Script####

$body = @()
$body += "Attention Robots, best check these servers be running!"
$body += "................................................................................................"
$body +=  foreach ($Server in $ServerName) {
                    if (test-Connection -ComputerName $Server -Count 2 -Quiet ) { 
                        $body += write-output "$Server is alive and responding to ping `n" 

                            } else { $body += Write-output "$Server unresponsive and not responding to ping `n" 

                            }    
        
}


$body = $body | out-string

$email = @{
 From = "
systems@robot.com
"
 To =
helpdesk@robot.com
 Subject = "Robot Server Status - Morning Check"
 SMTPServer = "exchangehubexample.robot.com"
 Body = $body
 }

send-mailmessage @email



Assuming all is well, the good folks who monitor the helpdesk inbox should recieve an email which looks like this:


Attention Robots, best check these servers be running!
................................................................................................

na-robot1 is alive and responding to ping

na-robot2 is alive and responding to ping

ms-dc2 is alive and responding to ping

 

No comments:

Post a Comment