VBScript for returning the last reboot time

Started by hjoseph

VBScript for returning the last reboot time   04 January 2019, 00:17

Good Morning!

I am attempting to run a script that returns the last reboot time. I found this script (author Alistair McMillan, 2011) :

Option Explicit

' Declare variables
Dim colItems, objItem, objWMIService
Dim strBoot, strBootDate, strBootDay, strBootHour, strBootMins
Dim strBootMonth, strBootTime, strBootYear, strComputer, strMsg, strQuery

strComputer = "."

If IsEmpty(strComputer) Then
	WScript.quit()
ElseIf	strComputer = "" Then
	strComputer = "."
End If

' Connect to specified computer
Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
Set colItems = objWMIService.ExecQuery( "Select * from Win32_OperatingSystem", , 48 )
For Each objItem in colItems
	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 )
	strBootMins  = Mid( objItem.LastBootUpTime, 11, 2 )
	strBootTime  = strBootHour & ":" & strBootMins
	strBoot = strBootDate & ", " & strBootTime
	strMsg  = strBoot
Next

' Display results
Output.write strMsg

'Done
'WScript.Quit(0)

The script returns the datetime of the last reboot of the computer running the script. I've been pouring over this and can't seem to find my error. Can anyone help?

(To clarify: the script reads the top three machines on my CSV correctly then adds multiple lines for one machine repeatedly.)
SoftPerfect Support forum - Andrew avatar image

Re: VBScript for returning the last reboot time   04 January 2019, 17:13

This script obviously connects to the a computer's WMI subsystem and parses an output of the following expression:
SELECT LastBootUpTime FROM Win32_OperatingSystem
Normally that returns a string like this in the yyyymmddHHMMSS.mmmmmmsUUU format (see CIM_DATETIME description):
20190104140000.111597+600
So to figure out why the script fails at a specific machine, run the above WMI query against it to see what it returns.

Alternatively, in the newly released version 7.1.9, you could perform a regex replacement to extract the data in a more readable format. For that head to Options - Remote WMI - New and add the above SELECT statement. It will return a raw timestamp value. Then apply the following regular expression to this column:
(\d{4})(\d{2})(\d{2})(\d{2})(\d{2}).*
Use this as the replacement expression:
\1-\2-\3 on \4:\5
These should give up the last boot-up time in a readable form:
SoftPerfect support forum

Re: VBScript for returning the last reboot time   05 January 2019, 06:56

Andrew,

Thanks so much! The RegEx method worked!

Sometimes you can get the answer faster if you try the forum search and/or have a look at the software user manual to see if your question has already been answered.

Our forum rules are simple:

  • Be polite.
  • Do not spam.
  • If possible, check your spelling and grammar.

Author:

Subject

A brief and informative title for your message, approximately 4–8 words:

     

Spam prevention: please enter the following code in the input field below.

  ******   **         *******   **    **        ** 
 **    **  **        **     **   **  **         ** 
 **        **        **     **    ****          ** 
 **        **         ********     **           ** 
 **        **               **     **     **    ** 
 **    **  **        **     **     **     **    ** 
  ******   ********   *******      **      ******  

Message: