VBscript to get the system uptime from a group of computers

One of our corporate customers doubted that we had computers being left powered on overnight.  So I quickly wrote the script below to query each computer’s uptime.

The record uptime?  93 days!

One co-worker remarked that Windows 95 & 98 had a bug which caused it to crash after 49.7 days.  I’m amazed any Windows 95/98 system would make it to 49 days, in the first place.

Set objFSO = CreateObject("Scripting.FileSystemObject")
If not objFSO.FileExists("C:\computer_lists\uptime_check.txt") Then
    WScript.Quit(99)
End If

Set ObjStream = objFSO.OpenTextFile("C:\computer_lists\uptime_check.txt",1)

Do While Not ObjStream.AtEndOfStream
   strComputer = ObjStream.ReadLine
   ' strComputer shouldn't be blank, if it is, there is something wrong with the input file.

   If strComputer = ""    Then
      WScript.Quit
   End If

   If Not Reachable(strComputer) Then
      WScript.Sleep 100
   End If
   
   If Reachable(strComputer) Then
      wscript.Echo upTime(strComputer) 
   End If

Loop
WScript.Echo "Finished"
WScript.Quit

Function Reachable(strComputer)
   On Error Resume Next
  
   Dim wmiQuery, objWMIService, objPing, objStatus
   wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputer & "'"
   Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
   Set objPing = objWMIService.ExecQuery(wmiQuery)
   For Each objStatus In objPing

     If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
        Reachable = False 'if computer is unreachable, return false
      Else
         Reachable = True 'if computer is reachable, return true
      End If
   
   Next 
End Function

'Code pinched from here: http://www.visualbasicscript.com/Get-current-user-and-uptime-m32298.aspx
Function upTime(strComputer)
Dim objOS
Dim dtmBootup
Dim dtmLastBootupTime
Dim dtmSystemUptime
Dim colOperatingSystems
Dim objOperatingSystem
On error Resume Next
upTime = 0
objWMIServices = "winmgmts:{impersonationLevel=impersonate}!//"& strComputer &""
Set objUserSet = GetObject( objWMIServices ).InstancesOf ("Win32_ComputerSystem")
Set colOperatingSystems = GetObject( objWMIServices ).InstancesOf ("Win32_OperatingSystem")

For Each objOS in colOperatingSystems
   dtmBootup = objOS.LastBootUpTime
   dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
   dtmSystemUptime = "Last system reboot occurred for "  & strComputer & " is:" & DateDiff("h", dtmLastBootUpTime, Now) & " hours, " & Int(DateDiff("n", dtmLastBootUpTime, Now)/60) & " minutes, " & DateDiff("n", dtmLastBootUpTime, Now) Mod 60 & " seconds ago." 
   'dtmSystemUptime = strComputer & "," & DateDiff("h", dtmLastBootUpTime, Now) & "," & Int(DateDiff("n", dtmLastBootUpTime, Now)/60) & "," & DateDiff("n", dtmLastBootUpTime, Now) Mod 60 & ",end" 
If Err.Number =0 Then
upTime = dtmSystemUptime
Else
upTime = "Last reboot time cannot be retrieved from " & strComputer
End If
Err.Clear
Next
End Function

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