WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Reboot Issues

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Get Start up and Shutdown Times of a Machine


Question:

I need to know the last time a pc was rebooted? Specifically I want to know the start up and shutdown times

Answer:

If you need the startup and shutdown times of the pc itself, we have a solution for you. You can use the eventprotocol from windows. everytime windows starts up, the event-protocol is started. this event is logged with the exact time in the event-viewer. Same with the shutdown of windows. With the following WMI script, you can filter the events and get the times you need.

Here is some WMI code that can query the event logs for that same information:

;Eventcode 6005 = The Event log service was started.
;Eventcode 6006 = The Event log service was stopped.

objSWbemLocator = GetObject("winmgmts:")
objEventSet = objSWbemLocator.ExecQuery("select * from Win32_NTLogEvent where Logfile='System' and EventCode='6005'") 
if objEventSet.Count == 0
   message("Notice","No Events")
   objEventSet = 0
   objSWbemLocator = 0
   exit
endif

foreach LogEvent in objEventSet 
   data = StrCat( "Event Number: ", LogEvent.RecordNumber, @CRLF ) 
   data = StrCat( data, "Log File: ", LogEvent.LogFile, @CRLF ) 
   data = StrCat( data, "Type: " , LogEvent.Type, @CRLF ) 
   data = StrCat( data, "Source: " , LogEvent.SourceName, @CRLF ) 
   data = StrCat( data, "Message: " , LogEvent.Message, @CRLF )
   DMTF_date = LogEvent.TimeWritten
   yr =  StrSub(DMTF_date,1,4)
   mnth  = StrSub(DMTF_date,5,2) 
   day = StrSub(DMTF_date,7,2)
   hr = StrSub(DMTF_date,9,2) 
   mins = StrSub(DMTF_date,11,2)
   secs = StrSub(DMTF_date,13,2)
   YMDHMS_date = StrCat(yr,":",mnth,":",day,":",hr,":",mins,":",secs)
   data = StrCat( data, "Time: " , YMDHMS_date, @CRLF )  
   data = StrCat( data, "Code: " , LogEvent.EventCode, @CRLF )  
   Message("Last Logon",StrCat("Event data:",@crlf,@crlf, data ))
   ;only grab first instance
   break
next 
objEventSet = 0
objSWbemLocator = 0
Exit



Article ID:   W17029
File Created: 2007:07:03:14:27:42
Last Updated: 2007:07:03:14:27:42