Monday, January 6, 2014

VBScript : Check Ping Status and Last boot time

Do you want to check status of multiple servers ? Do you want to know whether it is online ? or Do you want to check when was it rebooted? And on top of that you have 100+ servers to check... here is the solution.

Below is a VBScript which gives you Ping Status and last boot time for a list of servers.


Set objFSO = CreateObject("Scripting.FileSystemObject")
'Servers.txt is the input file. Save the server names in this file.
Set objTextFile = objFSO.OpenTextFile("C:\temp\Servers.txt",1,True)
'Result.txt is the output file
Set objOutputFile = objFSO.OpenTextFile("C:\temp\Result.txt",2,True)

Do While objTextFile.AtEndOfLine <> True
  strComputer = objTextFile.ReadLine


On Error resume Next
Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
Set oPing = oWMI.Get("Win32_PingStatus.Address='"& strComputer & "'")

If oPing.StatusCode = 0 Then
   Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")

For Each objOS in colOperatingSystems
    dtmBootup = objOS.LastBootUpTime
    Exit For

Next

WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
        Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) )
WMIDateStringToTime = CDate(Mid(dtmBootup, 9, 2) & ":" & _
        Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup,13, 2))

objOutputFile.WriteLine( strComputer & vbTab & WMIDateStringToDate & " " & WMIDateStringToTime & vbTab & "Able to Ping")

Else

objOutputFile.WriteLine( strComputer & vbTab & "Not Able to Ping")

End If

Set colOperatingSystems = nothing
Set objWMIService = Nothing
Set oWMI = Nothing
Set oPing = Nothing

Loop

Wscript.Echo "Done"

Note :

  • The script can be used to retrieve the values from servers in the same domain.
  • Save the script as a .vbs file and double click the same to run.
  • Servers.txt will store the list of servers for which output is required.
  • Result.txtis created which stored the output.
 Please write your queries in comment section.

No comments:

Post a Comment