Need a way of finding out when a computer last booted

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tbharber
Posts: 32
Joined: 17 Sep 2010 17:08

Need a way of finding out when a computer last booted

#1 Post by tbharber » 22 Sep 2010 14:35

Several of my coworkers and I have created a script that gives us some info on remote computers we are working on. Currently one of the pieces of information we need is finding out when a remote computer's uptime. We are using the code "Systeminfo /S %computername% | Find "Up Time""

For reasons I won't get into here this script is only working on about 65% of the computers we are testing. The other 35% come back with a "System Up Time: N/A" status. After searching Google, we believe this to be a problem with the WMI service of the computers.

With that said we need an alternate way of getting this information. We are aware there are plenty of third party tools to do this but due to company policy we can only use scripts.

Does anyone have any thoughts on how to get a systems up time other than the systeminfo command in Windows XP?

aGerman
Expert
Posts: 4693
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Need a way of finding out when a computer last booted

#2 Post by aGerman » 22 Sep 2010 16:53

Only an idea ...

Save as "uptime.vbs":

Code: Select all

On Error Resume Next
strComputer = WScript.Arguments(0)
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colEvents = oWMI.ExecQuery("Select * from Win32_NTLogEvent Where Logfile = 'System' and EventCode = '6005'")

For Each singleEvent In colEvents
  timestamp = singleEvent.TimeWritten
  Exit For
Next

WScript.Echo FormatDateTime(Now - CDate(DateSerial(Left(timestamp, 4), Mid(timestamp, 5, 2), _
  Mid(timestamp, 7, 2)) & " " & TimeSerial(Mid(timestamp, 9, 2), _
  Mid(timestamp, 11, 2), Mid(timestamp, 13, 2))))


Call as follows:

Code: Select all

@echo off &setlocal
for /f "delims=" %%a in ('cscript //nologo uptime.vbs "%computername%"') do echo %%a
pause


Regards
aGerman

tbharber
Posts: 32
Joined: 17 Sep 2010 17:08

Re: Need a way of finding out when a computer last booted

#3 Post by tbharber » 22 Sep 2010 18:07

Thanks. I will give that a try and see if it works out. The other idea I have been reading about online is using the date and time stamp of the Windows pagefile. Assuming it is in the same location on each computer I would think this would work. Does the pagefile date and time change for any other reason than a computer boot? I have only checked this out on a few machines but it seems accurate.

Post Reply