August 2nd, 2005

How Can I Determine the Uptime for a Server?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I determine the uptime for a server?

— LF

SpacerHey, Scripting Guy! AnswerScript Center

Hey, LF. This is an easy one. After all, we’re assuming you’re talking about a Windows server, and Windows servers never go down, right? Therefore, the uptime must be forever. Problem solved.

Well, OK, we suppose that maybe a Windows server could go down (probably because a non-Windows user snuck in and pulled the plug or something). If you need to know the system uptime on either a Windows XP or a Windows Server 2003 computer you can use a script like this:

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”) Set colOperatingSystems = objWMIService.ExecQuery _ (“Select * From Win32_PerfFormattedData_PerfOS_System”)

For Each objOS in colOperatingSystems intSystemUptime = Int(objOS.SystemUpTime / 60) Wscript.Echo intSystemUptime & ” minutes” Next

As you can see, we connect to the WMI service and then use the ExecQuery method to retrieve all instances of the Win32_PerfFormattedData_PerfOS_System class. (This, by the way, explains why the script runs only on Windows XP or Windows Server 2003: those are the only two versions of Windows that include the Win32_PerfFormattedData_PerfOS_System class.)

Win32_PerfFormattedData_PerfOS_System contains a number of performance counters related to the operating system, including SystemUpTime, which tells you how many second the machine has been running. We use this line of code to grab the value of SystemUpTime, divide it by 60 (thus giving us the uptime in minutes rather than seconds), and then convert it to an integer, stripping away anything after the decimal point:

intSystemUptime = Int(objOS.SystemUpTime / 60)

Yes, that is an awful lot for one little line of code to do, isn’t it? We then echo the value of the variable intSystemUptime and we’re done.

Well, unless you’re running Windows 2000, that is. Like we said, the preceding script won’t run on Windows 2000; Windows 2000 has never even heard of the Win32_PerfFormattedData_PerfOS_System class. Fortunately there is a workaround; it’s a bit clumsy, but it’s a workaround nevertheless:

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(“n”, dtmLastBootUpTime, Now) Wscript.Echo dtmSystemUptime & ” minutes” 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

In this script we connect to the WMI service and then query the Win32_OperatingSystem class; that’s because Win32_OperatingSystem includes a property – LastBootUpTime – that can tell you the last time the computer started. That’s useful: if we subtract LastBootUpTime from the current time we’ll know – that’s right – how long the computer has been running.

The only tricky part is the fact that, like all WMI dates and times, LastBootUpTime is stored in the UTC (Universal Time Coordinate) format. That means you’re going to get back a value that looks like this:

20050726071152.500000-420

Before you try, don’t bother subtracting that from the current date and time; it won’t work. Instead, we need to convert this into something that looks more like a real date and time; that’s why this line of code calls a function we wrote (WMIDateStringToDate) that converts a UTC datetime value into a regular old date-time value:

dtmLastBootupTime = WMIDateStringToDate(dtmBootup)

Note. We won’t explain how the function works. If you’re interested in that information, see this portion of the Microsoft Windows 2000 Scripting Guide.

After making the conversion we use the DateDiff function to subtract the last bootup time from the current time:

dtmSystemUptime = DateDiff(“n”, dtmLastBootUpTime, Now)

As you can see, we pass DateDiff three parameters:

“n”, which means to report the time difference in minutes.

dtmLastBootUpTime, a variable holding the converted datetime value.

Now, the current date and time.

All we do then is echo back the system uptime in minutes.

Like we said, we can’t imagine why you’d ever need a script like this for a Windows computer, but just in case ….

Author

0 comments

Discussion are closed.