Hey, Scripting Guy! Is there any way to tell whether or not a server has rebooted?
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)
WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & “/” & _
Mid(dtmBootup, 7, 2) & “/” & Left(dtmBootup, 4) _
& ” ” & Mid (dtmBootup, 9, 2) & “:” & _
Mid(dtmBootup, 11, 2) & “:” & Mid(dtmBootup, _
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.