Solved

How to find out the latest Windows start time

Posted on 2006-11-08
8
2,316 Views
Last Modified: 2011-10-03
I need a vbs script that I can use on any arbitrary Windows XP system to discover the precise date and time that the system was most recently started.  I'll be placing this script file on a USB key to carry to my clients' computers.  Also, I'd like to know whether I can discover this answer somewhere within the registry or the event log, without a script.  The first provider of the best script will get the points.
0
Comment
Question by:JohnAtkinson
  • 4
  • 3
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 17903822
You can find the boot time in the event log by looking for the most recent ID6009 event (http://www.eventid.net/source.asp?source=EventLog) which indicates when the event log service is started on bootup.
0
 

Author Comment

by:JohnAtkinson
ID: 17903911
Is it possible for the information to be surfaced by a script?
0
 
LVL 34

Accepted Solution

by:
sramesh2k earned 500 total points
ID: 17904181
Hi John,

You may use the Uptime.exe utility from Microsoft.

Determine the current system uptime:
http://www.winxptutor.com/uptime.htm

0
 

Author Comment

by:JohnAtkinson
ID: 17921949
It sure would be great to have this info delivered by a script, showing the actual time of the reboot, rather than the length of time the system has been up.  Maybe it's not possible, though.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 34

Expert Comment

by:sramesh2k
ID: 17924311
Hi John,

To get the actual time (when the system is started), you can use this script, a modified version of the one found at MS scripting site:

Hey, Scripting Guy! How Can I Tell if a Server has Rebooted?:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0907.mspx

Is modified as:
- - -
strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
    dtmBootup = objOS.LastBootUpTime
    dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
    'dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
    Wscript.Echo dtmLastBootupTime
    Wscript.Echo dtmSystemUptime
Next
- - -

Sample output
=========

11/12/2006 11:36:48 AM

***** script completed *****
0
 
LVL 34

Expert Comment

by:sramesh2k
ID: 17924312
- - -
strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
    dtmBootup = objOS.LastBootUpTime
    dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
    Wscript.Echo dtmLastBootupTime
Next
- - -

Sample output
=========

11/12/2006 11:36:48 AM

***** script completed *****
0
 

Author Comment

by:JohnAtkinson
ID: 17932367
Thanks for the script, sramesh2k.  Unfortunately, I already accepted the previous answer, thinking that there was no scripting solution.  I don't know how to re-assign the points, or whether that's even possible, or allowed.  

In any case, I took your script, which wouldn't work without the WMIDateStringToDate function, and fleshed it out to report the time of last reboot in both absolute terms and in relative terms.  For the benefit of the community, I'm posting the script here.

------------------------------ cut here --------------------------------------
Function WMIDateStringToDate(dtmBootup)
WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
      Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
      & " " & Mid (dtmBootup, 9, 2) & ":" & _
      Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
      13, 2))
End Function

strComputer = "."
Dim msg, HoursInPartialDay, MinutesInPartialHour, SecondsInPartialMinute
Dim Days, Hours, Minutes, Seconds
Dim DayMsg, HourMsg, MinuteMsg, SecondMsg
msg = ""
Set objWMIService = GetObject _
      ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
      ("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
      dtmBootup = objOS.LastBootUpTime
      dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
      Days = DateDiff("d", dtmLastBootUpTime, Now)
      Hours = DateDiff("h", dtmLastBootUpTime, Now) Mod 24
      Minutes = DateDiff("n", dtmLastBootUpTime, Now) Mod 60
      Seconds = DateDiff("s", dtmLastBootUpTime, Now) Mod 60
      
      Dim NumberOfComponents
      NumberOfComponents = 0
      
      If Days = 0 Then
            DayMsg = ""
      Else
            NumberOfComponents = NumberOfComponents + 1
            If Days = 1 Then
                  DayMsg = "1 day"
            Else
                  DayMsg = Days & " days"
            End If
      End If
      
      If Hours = 0 Then
            HourMsg = ""
      Else
            NumberOfComponents = NumberOfComponents + 1
            If Hours = 1 Then
                  HourMsg = "1 hour"
            Else
                  HourMsg = Hours & " hours"
            End If
      End If

      If Minutes = 0 Then
            MinuteMsg = ""
      Else
            NumberOfComponents = NumberOfComponents + 1
            If Minutes = 1 Then
                  MinuteMsg = "1 minute"
            Else
                  MinuteMsg = Minutes & " minutes"
            End If
      End If
      
      If Seconds = 0 Then
            SecondMsg = ""
      Else
            NumberOfComponents = NumberOfComponents + 1
            If Seconds = 1 Then
                  SecondMsg = "1 second"
            Else
                  SecondMsg = Seconds & " seconds"
            End If
      End If

      'Format the message in natural language.
      Select Case NumberOfComponents
            Case 0
                  msg = ""
            Case 1
                  msg = "That's " & DayMsg & HourMsg & MinuteMsg & SecondMsg & " ago."
            Case 2
                  msg = ""
                  If Days > 0 Then
                        msg = "That's " & DayMsg & " and "
                  End If
                  If Hours > 0 Then
                        If Len(msg) = 0 Then
                              msg = "That's " & HourMsg & " and "
                        Else
                              msg = HourMsg & " ago."
                        End If
                  End If
                  If Minutes > 0 Then
                        If Len(msg) = 0 Then
                              msg = "That's " & MinuteMsg & " and "
                        Else
                              msg = MinuteMsg & " ago."
                        End If
                  End If
                  If Seconds > 0 Then
                        msg = SecondMsg & " ago."
                  End If
            Case Else                        
                  msg = ""
                  If Seconds > 0 Then
                        msg = " and " & SecondMsg & " ago."
                  End If
                  If Minutes > 0 Then
                        If Len(msg) > 0 Then
                              msg = MinuteMsg & ", " & msg
                        Else
                              msg = " and " & MinuteMsg & " ago."
                        End If
                  End If
                  If Hours > 0 Then
                        If Len(msg) > 0 Then
                              msg = HourMsg & ", " & msg
                        Else
                              msg = " and " & HourMsg & " ago."
                        End If
                  End If
                  If Days > 0 Then
                        If Len(msg) > 0 Then
                              msg = DayMsg & ", " & msg
                        Else
                              msg = " and " & DayMsg & " ago."
                        End If
                  End If
                  msg = "That's " & msg
      End Select

      Wscript.Echo "The date and time of the latest system restart was " &  dtmLastBootupTime & vbCRLF & vbCRLF & msg
Next
------------------------------ cut here --------------------------------------
0
 

Author Comment

by:JohnAtkinson
ID: 17934277
Found a bug in that previous version of the script.  The updated version is provided below.



'strComputer = "."
Function WMIDateStringToDate(dtmBootup)
WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
      Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
      & " " & Mid (dtmBootup, 9, 2) & ":" & _
      Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
      13, 2))
End Function

strComputer = "."
Dim msg, HoursInPartialDay, MinutesInPartialHour, SecondsInPartialMinute
Dim Days, Hours, Minutes, Seconds
Dim DayMsg, HourMsg, MinuteMsg, SecondMsg
msg = ""
Set objWMIService = GetObject _
      ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
      ("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
      dtmBootup = objOS.LastBootUpTime
      dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
      Days = DateDiff("d", dtmLastBootUpTime, Now)
      Hours = DateDiff("h", dtmLastBootUpTime, Now) Mod 24
      Minutes = DateDiff("n", dtmLastBootUpTime, Now) Mod 60
      Seconds = DateDiff("s", dtmLastBootUpTime, Now) Mod 60
      Dim NumberOfComponents
      NumberOfComponents = 0
      
      If Days = 0 Then
            DayMsg = ""
      Else
            NumberOfComponents = NumberOfComponents + 1
            If Days = 1 Then
                  DayMsg = "1 day"
            Else
                  DayMsg = Days & " days"
            End If
      End If
      
      If Hours = 0 Then
            HourMsg = ""
      Else
            NumberOfComponents = NumberOfComponents + 1
            If Hours = 1 Then
                  HourMsg = "1 hour"
            Else
                  HourMsg = Hours & " hours"
            End If
      End If

      If Minutes = 0 Then
            MinuteMsg = ""
      Else
            NumberOfComponents = NumberOfComponents + 1
            If Minutes = 1 Then
                  MinuteMsg = "1 minute"
            Else
                  MinuteMsg = Minutes & " minutes"
            End If
      End If
      
      If Seconds = 0 Then
            SecondMsg = ""
      Else
            NumberOfComponents = NumberOfComponents + 1
            If Seconds = 1 Then
                  SecondMsg = "1 second"
            Else
                  SecondMsg = Seconds & " seconds"
            End If
      End If

      'Format the message in natural language.
      Select Case NumberOfComponents
            Case 0
                  msg = ""
            Case 1
                  msg = "That's " & DayMsg & HourMsg & MinuteMsg & SecondMsg & " ago."
            Case 2
                  msg = ""
                  If Days > 0 Then
                        msg = "That's " & DayMsg & " and "
                  End If
                  
                  If Hours > 0 Then
                        If Len(msg) = 0 Then
                              msg = "That's " & HourMsg & " and "
                        Else
                              msg = msg & HourMsg & " ago."
                        End If
                  End If
                  
                  If Minutes > 0 Then
                        If Len(msg) = 0 Then
                              msg = "That's " & MinuteMsg & " and "
                        Else
                              msg = msg &  MinuteMsg & " ago."
                        End If
                  End If
                  
                  If Seconds > 0 Then
                        msg = msg & SecondMsg & " ago."
                  End If
            Case Else                        
                  msg = ""
                  If Seconds > 0 Then
                        msg = " and " & SecondMsg & " ago."
                  End If
                  If Minutes > 0 Then
                        If Len(msg) > 0 Then
                              msg = MinuteMsg & ", " & msg
                        Else
                              msg = " and " & MinuteMsg & " ago."
                        End If
                  End If
                  If Hours > 0 Then
                        If Len(msg) > 0 Then
                              msg = HourMsg & ", " & msg
                        Else
                              msg = " and " & HourMsg & " ago."
                        End If
                  End If
                  If Days > 0 Then
                        If Len(msg) > 0 Then
                              msg = DayMsg & ", " & msg
                        Else
                              msg = " and " & DayMsg & " ago."
                        End If
                  End If
                  msg = "That's " & msg
      End Select

      Wscript.Echo "The date and time of the latest system restart was " &  dtmLastBootupTime & vbCRLF & vbCRLF & msg
Next
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If your system is showing symptoms of browser hijacks or 'google search redirects' check out my other article (http://rdsrc.us/u3GP7A) first and run the tool TDSSKiller (http://rdsrc.us/GDBBs4) to get rid of the infection. Once done, and if the …
Issue: Unstable cursor in Windows XP and Windows runs extremely slow in that any click will bring up the Hour glass (sometimes for several seconds before giving you what you want) . Troubleshooting Process and the FINAL FIX: This issue see…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now