[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to find out the latest Windows start time

Posted on 2006-11-08
8
Medium Priority
?
2,324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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
 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

649 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