Product and service reviews are conducted independently by our editorial team, but we sometimes make money when you click on links. Learn more.

How to Create an Accurate Server Uptime Tool in PowerShell

By - Source: Toms IT Pro

Overall server uptime is a common metric that lots of server administrators use to gauge overall availability. But an uptime metric is sometimes hard to generate if an administrator has to work with hundreds or thousands of servers. We need a quick, free way to generate a report of total uptime for all servers in an environment. What better tool to solve such a problem than PowerShell?

By using a PowerShell script, we can poll as many servers as necessary and generate a fancy HTML report or just a CSV file containing total uptime for each of our servers. But first, we need to figure out how to capture the uptime on a single server.

MORE: Windows 10 for IT Pros: Tips, Tricks and Tutorials

There are some ways to get server uptime even without logging into every server. We can use WMI, the registry or the event log. The most accurate way to determine uptime is via the event log. Whenever a server starts up and shuts down, an event is generated inside of the System event log. By parsing the event log, we can capture the start and end events that the server generates and compare the two times to create a total uptime. Also, since multiple instances of these events are stored, we can also query a history of uptimes.

To expedite the process, we can download a community script called Get-ServerUptimeReport. This script allows you to simply provide a computer name as a parameter where it will then parse the System event log of the computer and attempt to find both a start and stop event to compare the two. It will then return the total time the server was online until the event log has rolled.

Here's an example of using this script on a server:

PS C:> Get-ServerUptimeReport.ps1 -ComputerName sqlsrv1

Startup                          Shutdown              Uptime (Days) Uptime (Min)
 -------                            --------                  -------------    ------------
9/16/2017 12:40:00 PM 9/22/2017 4:20:11 PM       6.15      8860.18
9/16/2017 10:22:49 AM 9/16/2017 12:22:36 PM     0.08      119.79
9/16/2017 3:22:12 PM  9/22/2017 4:20:11 PM        6.04      8697.98

This is great for a quick way to find the uptime of a single server. But what if we need this information for lots of servers at once? To do this, we can gather up a list of servers and then pass each computer name one at a time to this script. Because the output above will be representing lots of servers, we'll also have to add a custom property to the output to indicate which server the output is coming from.

As an example, for now, I'm just going to define all servers in an array in my PowerShell console. In reality, you might be pulling server names from Active Directory, Hyper-V or a text file. I'll first define all of the server names and then iterate over each one with a loop.

$servers = 'WEBSRV1','SQLSRV1'
foreach ($server in $servers) {
   Get-ServerUptimeReport.ps1 -ComputerName $server

Startup                             Shutdown                  Uptime (Days)   Uptime (Min)
 -------                               --------                     -------------       ------------
9/16/2017 12:45:48 PM   9/22/2017 4:25:39 PM        6.15             8859.86
9/16/2017 10:42:52 AM   9/16/2017 12:42:34 PM      0.08             119.7
9/16/2017 2:42:17 PM     9/22/2017 4:25:39 PM        6.07             8743.38
9/16/2017 12:40:00 PM   9/22/2017 4:25:39 PM        6.16             8865.65
9/16/2017 10:22:49 AM   9/16/2017 12:22:36 PM      0.08             119.79
9/16/2017 3:22:12 PM    9/22/2017 4:25:39 PM         6.04             8703.46

This works, but we can't determine which server each row references. Let's add a server name to the output.

$servers = 'WEBSRV1','SQLSRV1'
foreach ($server in $servers) {
    Get-ServerUptimeReport.ps1 -ComputerName $server | Select-Object -Property *,@{n='ServerName';e={$server}} | Format-Table -AutoSize

Startup                           Shutdown                      Uptime (Days) Uptime (Min) ServerName
 -------                             --------                         -------------     ------------     ----------
9/16/2017 12:45:48 PM   9/22/2017 4:34:59 PM        6.16            8869.19       WEBSRV1
9/16/2017 10:42:52 AM   9/16/2017 12:42:34 PM      0.08            119.7          WEBSRV1
9/16/2017 2:42:17 PM    9/22/2017 4:34:59 PM         6.08           8752.71       WEBSRV1

Startup                            Shutdown                     Uptime (Days)   Uptime (Min) ServerName
 -------                              --------                         -------------       ------------    ----------
9/16/2017 12:40:00 PM   9/22/2017 4:35:01 PM         6.16             8875.01        SQLSRV1
9/16/2017 10:22:49 AM   9/16/2017 12:22:36 PM       0.08             119.79          SQLSRV1
9/16/2017 3:22:12 PM     9/22/2017 4:35:01 PM         6.05            8712.81         SQLSRV1

We now have a nice, little tool that can provide us a quick report on uptime for our servers over time.