' Declare variables Dim strBoot, strBootDay, strBootHour, strBootMins, strBootMonth, strBootYear Dim strComputer, strMsg ' Check command line parameters Select Case WScript.Arguments.Count Case 0 ' Default if none specified is local computer (".") Set objWMIService = GetObject( "winmgmts://./root/cimv2" ) Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem", , 48 ) For Each objItem in colItems strComputer = objItem.Name Next Case 1 ' Command line parameter can either be a computer ' name or "/?" to request online help strComputer = UCase( Wscript.Arguments(0) ) if InStr( strComputer, "?" ) > 0 Then Syntax Case Else ' Maximum is 1 command line parameter Syntax End Select ' Enable error handling On Error Resume Next ' Connect to specified computer Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" ) ' Display error number and description if applicable If Err Then ShowError Set colItems = objWMIService.ExecQuery( "Select * from Win32_OperatingSystem", , 48 ) For Each objItem in colItems strTimeShift = Right( objItem.LastBootUpTime, 4 ) / 60 strBootYear = Left( objItem.LastBootUpTime, 4 ) strBootMonth = Mid( objItem.LastBootUpTime, 5, 2 ) strBootDay = Mid( objItem.LastBootUpTime, 7, 2 ) strBootDate = DateValue( strBootDay & "-" & strBootMonth & "-" & strBootYear ) strBootHour = Mid( objItem.LastBootUpTime, 9, 2 ) - strTimeShift strBootMins = Mid( objItem.LastBootUpTime, 11, 2 ) If strBootHour < 0 Then strBootHour = strBootHour + 24 strBootDate = DateAdd( "d", -1, DateValue( strBootDate ) ) End If If strBootHour > 23 Then strBootHour = strBootHour - 24 strBootDate = DateAdd( "d", 1, DateValue( strBootDate ) ) End If strBootTime = strBootHour & ":" & strBootMins strBoot = strBootDate & ", " & strBootTime strMsg = "Last boot time of " & strComputer & ": " & strBoot Next ' Display results WScript.Echo strMsg 'Done WScript.Quit(0) Sub ShowError() strMsg = vbCrLf & "Error # " & Err.Number & vbCrLf _ & Err.Description & vbCrLf & vbCrLf & vbCrLf Syntax End Sub Sub Syntax() strMsg = strMsg _ & "LastBoot.vbs, Version 1.20" & vbCrLf _ & "Display last boot time for any WMI " _ & "enabled computer on the network" & vbCrLf & vbCrLf _ & "Usage: CSCRIPT LASTBOOT.VBS [ computer_name ]" _ & vbCrLf & vbCrLf _ & "Where: " & Chr(34) & "computer_name" & Chr(34) _ & " is the optional name of a remote computer" & vbCrLf _ & " (default is the local computer " _ & "name)" & vbCrLf & vbCrLf _ & "Note: Date and time will be in DD-MM-YYYY and HH:mm format," _ & vbCrLf _ & " and based on the remote computer's time zone settings." _ & vbCrLf _ & " Modify strBoot variable if you want a different format." _ & vbCrLf & vbCrLf _ & "Written by Rob van der Woude" & vbCrLf _ & "http://www.robvanderwoude.com" & vbCrLf & vbCrLf _ & "Created with Microsoft's Scriptomatic tool" & vbCrLf _ & "http://www.microsoft.com/technet/treeview/default.asp" _ & "?url=/technet/scriptcenter/WMImatic.asp" & vbCrLf WScript.Echo strMsg WScript.Quit(1) End Sub