' Enable custom error handling On Error Resume Next ' Declare variables Dim numUptime, numUptMins, numUptHour, numUptDays Dim strYear, strMonth, strDay, strDate, strHour, strMins, strTime Dim strComputer, strMsg ' Check command line parameters If WScript.Arguments.Named.Count > 0 Then Syntax End If Select Case WScript.Arguments.Unnamed.Count Case 0 strComputer = "." Case 1 strComputer = Wscript.Arguments(0) Case Else Syntax End Select ' Connect to the specified computer Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" ) ' Display error number and description if applicable If Err Then ShowError ' Get the "real" computer name Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem", , 48 ) ' Display error number and description if applicable If Err Then ShowError For Each objItem in colItems strComputer = objItem.Name Next ' Get the uptime by querying the boot time and the current time Set colItems = objWMIService.ExecQuery( "Select * from Win32_OperatingSystem", , 48 ) For Each objItem in colItems numUptime = DateDiff( "n", _ ParseDat( objItem.LastBootUpTime ), _ ParseDat( objItem.LocalDateTime ) ) numUptMins = numUptime Mod 60 numUptHour = ( numUptime \ 60 ) Mod 24 numUptDays = ( numUptime \ 60 ) \ 24 ' Build message to be displayed ' Day(s) strMsg = vbCrLf & strComputer & " has been up for " & numUptDays & " day" ' Add "s" for multiple days If numUptDays <> 1 Then strMsg = strMsg & "s" ' Hour(s) strMsg = strMsg & ", " & numUptHour & " hour" ' Add "s" for multiple hours If numUptHour <> 1 Then strMsg = strMsg & "s" ' And minute(s) strMsg = strMsg & " and " & numUptMins & " minute" ' Add "s" for multiple minutes If numUptMins <> 1 Then strMsg = strMsg & "s" Next ' Display the result Wscript.Echo strMsg Function ParseDat( ByVal strDate ) strYear = Left( strDate, 4 ) strMonth = Mid( strDate, 5, 2 ) strDay = Mid( strDate, 7, 2 ) strDat = strDay & "-" & strMonth & "-" & strYear strHour = Mid( strDate, 9, 2 ) - strTimeShift strMins = Mid( strDate, 11, 2 ) strTime = strHour & ":" & strMins ParseDat = strDat & " " & strTime End Function Sub ShowError strMsg = strMsg & vbCrLf _ & "Error " & CStr( Err.Number ) _ & ": " & Err.Description & vbCrLf Err.Clear Syntax End Sub Sub Syntax strMsg = strMsg & vbCrLf _ & "UpTime.vbs, Version 1.00" & vbCrLf _ & "Display uptime for any WMI enabled " _ & "computer on the network." & vbCrLf & vbCrLf _ & "Usage: CSCRIPT UPTIME.VBS [ computer_name ]" _ & vbCrLf & vbCrLf _ & "Where: " & Chr(34) & "computer_name" & Chr(34) _ & " is the name of a WMI enabled computer on the network" _ & vbCrLf & vbCrLf _ & "Written by Rob van der Woude" & vbCrLf _ & "http://www.robvanderwoude.com" & vbCrLf & vbCrLf _ & "Created with Microsoft's Scriptomatic 2.0 tool" & vbCrLf _ & "http://www.microsoft.com/downloads/details.aspx?" & vbCrLf _ & " FamilyID=09dfc342-648b-4119-b7eb-783b0f7d1178&DisplayLang=en" & vbCrLf WScript.Echo strMsg WScript.Quit(1) End Sub