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!

14 replies »

  1. Hello Patrick,

    I run your script, but script do not work. I did not have any result, script ran and that it is.

    Like

  2. Ok I’m lost 🙂
    I’ve followed the steps exactly, and I’m running the script from a domain controller, of which I’m a domain admin.
    The command just executes without any returning anything.
    I’ve also copied the script to the mentioned folder C:\Program Files\WindowsPowerShell\Modules etc.

    What am I missing?
    Thanks!

    Like

  3. But this script gives the uptime from when the server is running continuously, if it has been restarted it does not calculate the data before restarting, right? Is there a way to get the monthly uptime and downtime of the servers?

    Like

  4. This doesn’t work. I don’t know if I’m supposed to import certain modules first or what, but it just executes, and then I’m sitting at C:\> _ . No output is displayed. Do I need to run it as domain admin? From a DC? Elevated rights?

    Like

  5. Hello Patrick!
    Awesome script. I want to use this in my AD. I do not need all servers to be checked but just a few ones of certain branchoffices which are splitted in several OUs. How does the script has to be modified to get this done?

    Thank you for your help and best regards
    Ronny

    Like

  6. Hello Patrick, here is again. Tell me please explaint in your script what means $result=@()

    Dose mena that variabla $result is ste as array. In your script array of variable computers

    Thanks

    Like

  7. 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 comment

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