Wednesday, September 14, 2011

VB Script to Ping servers and send email notification when server is not reachable.

'**************************************
' Author Name: Honnappa Jadi
' Designation : SQL DBA
' Date: 14-Sep-2011
' Description: This script will ping all the servers mentioned in this and if any server not pingable it will send email notification.
' Inputs : Mention all your server's IP addresses in the strComputers with separated by comma and mention your/group email id in ToAddress
'**************************************
Dim ToAddress
Dim FromAddress
Dim MessageSubject
Dim MessageBody
Dim ol, ns, newMail
ToAddress = "ABC@XYZ.com"
strComputers = "Server 1 IP Addreess,Server 2 IP Addreess,.....,Server n IP Address")
arrComputers = split(strComputers, ",")
For Each strComputer in arrComputers
 Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
          ExecQuery("select * from Win32_PingStatus where address = '" & _
              strComputer & "'")
 For Each objPingStatus in objPing
         If IsNull(objPingStatus.StatusCode) or objPingStatus.StatusCode<>0 Then
   if strFailedPings <> "" then strFailedPings = strFailedPings & vbcrlf
   strFailedPings = strFailedPings & strComputer
         End If
     Next
Next
IF strFailedPings = "" then
 'wscript.echo "All servers are up and running"
ELSE
MessageSubject = "Server Not Reachable !!!!!"
MessageBody = "The following servers are not reachable" & _
  vbcrlf & vbcrlf & strFailedPings
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf
newMail.RecipIents.Add(ToAddress)
newMail.Send
Set ol = Nothing
Set ns = Nothing
END IF