How Can I Tell if a Server has Rebooted?

How Can I Tell if a Server has Rebooted?

  • Comments 4
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! Is there any way to tell whether or not a server has rebooted?

-- MvdM

SpacerHey, Scripting Guy! AnswerScript Center

Hey, MvdM. We’re assuming that you’re performing a task and, at the end of the task, the computer is supposed to reboot. Needless to say, you want to know, “Well, did the server reboot?” How can you tell whether the machine restarted or not?

The easiest way is to check the system uptime and see how long the computer has been running. Suppose your procedure was supposed to run at midnight, and when you come to work at 8:00 AM you want to know whether the computer restarted or not. Well, if it did, then the system uptime should be about 8 hours or so. If it is, then the odds are pretty good that the computer rebooted; if the system uptime is appreciably longer than 8 hours, then it probably didn’t reboot.

So how do you determine system uptime? That’s easy: just use the WMI class Win32_OperatingSystem and check the value of the LastBootupTime property. Of course, there are two minor problems with that approach. First, the LastBootupTime property tells you when the computer last restarted; however, it doesn’t tell you how long ago that was. Second, like all WMI date-time properties, LastBootupTime is reported in UTC format, meaning you get back a date that looks like this: 200409070130.000000+480. Yuck.

But that’s OK; just make sure your script converts the UTC date to a standard date-time format, and then use VBScript’s DateDiff function to subtract the current date and time from the last bootup date and time; that will tell you how long the computer has been running since its last reboot. Here’s a sample script that does all those things:

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

Note two important points here. First, we have the function WMIDateStringToDate that converts the UTC date to a standard date-time value. We won’t bother to explain all the code here, but all we’re doing is teasing apart the UTC date-time values and then rearranging them in standard format. For example, with the UTC date 200409070130.000000+480 the first four digits represent the year. Thus our function takes the first four digits - Left(dtmBootup, 4) - and assigns that to the year portion of our standard date. We put the rest of our date-time value together using the same approach.

Second, note the use of the DateDiff function to subtract the last bootup time from the current time. In this example, we’re determining how many hours the server has been running; hence the parameter “h”, which reports time back in hours. Had we wanted to know the time in minutes, we would have used the “m” parameter. All in all pretty easy.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • You can use the script with a interesting tweak that doesn't require the function:

    (it uses the wbemscripting.swbemdatetime to get the date)

    strComputer = "."

    Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")

    Set objWMIService = GetObject _

       ("winmgmts:\\" & strComputer & "\root\cimv2")

    Set colOperatingSystems = objWMIService.ExecQuery _

       ("Select * from Win32_OperatingSystem")

    For Each objOS in colOperatingSystems

       objSWbemDateTime.Value = objOS.LastBootUpTime

       dtmBootup = objOS.LastBootUpTime

       dtmLastBootupTime = objSWbemDateTime.GetVarDate(false)

       dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)

       Wscript.Echo dtmSystemUptime

    Next

  • I found this post searching for a script to return uptime for a computer or server. I have modified it so that it prompts you for the computer name or the IP Address. Also, instead of returning just a number, I've added the word "hours" to the end and also had it show days as well. One caveat I noticed is that the machine you run this from needs to be a member of the target domain in order to gather that information. If not, a permission denied error is displayed. I tested this on both a Server 2003 machine and also my Windows 7 x64 laptop (which is where I discovered the permissions issue)

    ' Prompt user for the computer's name or IP Address

    UserInput = InputBox( myPrompt, "Enter a computer name or IP Address" )

    strComputer = UserInput

    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)

    ' Modified output to show hours and covert to days.

       Wscript.Echo "System uptime is " & dtmSystemUptime & " hours" & " (or " & (dtmSystemUptime/24) & " days)"

    Next

    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

  • Go to cmd and give the command 'net statistics server'.

    Look for the line which says Statistics since. That will be the reboot time

  • I use an online and free tool to monitor my servers uptime, Port Monitor. http://www.port-monitor.com