2.16. Viewing System Uptime

Problem

You want to find out how long a server has been running since the last restart.

Solution

Using a command-line interface

All three of the following commands display the system uptime:

> psinfo \\<ServerName> | findstr Uptime
> srvinfo \\<ServerName> | findstr /c:"Up Time"
> systeminfo /s <ServerName> | findstr /c:"Up Time"

Using VBScript

' This code prints system uptime for a host.
' ------ SCRIPT CONFIGURATION ------
strComputer = "."   ' e.g., rallen-srv01
' ------ END CONFIGURATION ---------
set objWMIDateTime = CreateObject("WbemScripting.SWbemDateTime")
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colOS = objWMI.InstancesOf("Win32_OperatingSystem")
for each objOS in colOS
   objWMIDateTime.Value = objOS.LastBootUpTime
   Wscript.Echo "System Up Time: " & objWMIDateTime.GetVarDate
next

Discussion

In the early days of Windows NT 3.51 and 4.0, it wasn't uncommon for servers to reboot or need to be rebooted on a regular basis. Applications were not as well behaved as they are today and the operating system wasn't as stable. This changed with Windows 2000, when unexpected failures became much less common. But there were still a lot of hotfixes and patches to install on a regular basis, most of which required reboots. Hopefully Windows Server 2003 will continue the upward trend in availability and further reduce the number of reboots that are required.

It is a good idea to pay attention to the uptime on your servers to make sure their ...

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.