Wednesday, 28 August 2013

VBS Script to Discover Last Reboot Time of Multiple Machines/Servers

On occassion you may wish to find the last reboot time of any given number of machines/servers on your network (for example if you need to check how many have rebooted following a patch update). The following script, cribbed from a technet article, works well and spits out a text file listing all the servers, their last reboot time and how many hours the system has been up for.
 
1]
create a text file listing all the servers/computers you want info on and name it servers.txt
 
2]
copy the below text into notepad and save it as ServerReboot.vbs. Be sure to save it into the same folder as the servers.txt file.

    ' =====================================================================
Set objTextFile = objFSO.OpenTextFile("c:\scripts\servers.txt", ForReading)
Set outfile = objFSO.CreateTextFile("Report.txt")
Do Until objTextFile.AtEndOfStream
    strComputer = objTextFile.Readline
    ' ===============================================================================
    ' Code to get the Last Boot Time using LastBootupTime from Win32_Operating System
    ' ===============================================================================
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
    dtmBootup = objOS.LastBootUpTime
    dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
    'OutFile.WriteLine "=========================================="
    OutFile.WriteLine "Computer: " & strComputer
    OutFile.WriteLine "Last Reboot: " & dtmLastBootupTime
    dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)  
    OutFile.WriteLine "System is online since " & dtmSystemUptime & " hours"
    OutFile.WriteLine "=========================================="
   
   
Next
    ' =====================================================================
    ' End
    ' =====================================================================
Loop
objTextFile.Close
 ' ===============================================================================
 ' Displaying to the user that the script execution is completed
 ' ===============================================================================
MsgBox "Script Execution Completed. The Report is saved as Report.txt in the current directory"
 ' ===============================================================================
 ' Function to convert UNC time to readable format
 ' ===============================================================================
Function WMIDateStringToDate(dtmBootup)
    WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
         Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
         & " " & Mid (dtmBootup, 9, 2) & ":" & _
         Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
         13, 2))
End Function

3]
open up the cmd prompt and execute the vbs file.
 
4]
wait for the script to chug along and then spit out a report.txt file.

Low and behold a list of servers/computers has been generated along with their last reboot times.


For further info see the following page: http://blogs.technet.com/b/manojnair/archive/2010/03/30/vbscript-to-find-out-last-reboot-time-of-multiple-computers.aspx

No comments:

Post a Comment