PowerShell

PowerShell: Showing the Uptime of all Windows Servers

Let’s say you want to get an overview of the uptime of all Windows servers in your domain. This is a very important information and you should have a look at the uptime of your services on a regular basis. Do you remember the 5 nines? What I’m talking about is the 99,999 % uptime of services. That value is well known as a baseline for good services.

The Goal

I like it to start with the goal first. The reader then has the chance to see what the goal is and to decide if it’s worth to read further on. As promised, I will give you a Powershell command in hand that gets all uptimes from all enabled domain joined Windows Servers. I’ve created a small function without any parameters, called Get-UpTimeAllServer.

The output will look like this:

1.png

If one of your servers cannot be reached due to network failure, missing WinRM configuration or for any other reason, then the server will be tagged as “Server could not be reached”.

2.png

The Function

All the shown above is wrapped into a function. Here it is:


function Get-UpTimeAllServer {

# .SYNOPSIS
# Get-UpTimeAllServer is an advanced Powershell function. It shows the uptime of all domain joined and enabled Windows servers.

# .DESCRIPTION
# Uses Get-CimInstance and a Try/Catch block.

# .PARAMETER
# None

# .EXAMPLE
# Get-UpTimeAllServer

# .NOTES
# Author: Patrick Gruenauer
# Web: https://sid-500.com

$servers=(Get-ADComputer -Filter 'operatingsystem -like "*server*"-and enabled -eq "true"').Name
$result=@()

Foreach ($s in $servers) {

Try {

$up=(Get-CimInstance -ClassName win32_operatingsystem -ComputerName $s -ErrorAction Stop).LastBootUpTime
$uptime=((Get-Date) - $up)
$result+=New-Object -TypeName PSObject -Property ([ordered]@{
'Server'=$s
'LastBootUpTime'=$up
'Days'=$uptime.Days
'Hours'=$uptime.Hours
'Minutes'=$uptime.Minutes
'Seconds'=$uptime.Seconds
})
}
Catch {

$result+=New-Object -TypeName PSObject -Property ([ordered]@{
'Server'=$s
'LastBootUpTime'='Server could not be reached'
})

}

You can download the function here:

Get-UpTimeAllServer

Run this script in PowerShell or copy it to

C:\Program Files\WindowsPowerShell\Modules

Create a Get-UpTimeAllServer folder there and save the downloaded file as a PSM1 file. Then the command is available every time you start PowerShell.

1.PNG

See you next time with some cool Powershell stuff!

3 replies »

  1. Hi Patrick,
    The function is very good, but it’s only for one domain. When you have several domains without a trust relationship from which you must recover the information, you must add the script that asks for the authentication of each domain so that this function works correctly.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.