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:
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”.
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.
See you next time with some cool Powershell stuff!
Categories: PowerShell, Windows Server
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!
LikeLike
its not giving any out just execute the command that`s all
LikeLike
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?
LikeLike
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?
LikeLike
Yes you need to run it from dc or client with rsat
LikeLike
Ah, my problem was it simply doesn’t work being run standalone after being unzipped. After I moved it to the proper location mentioned in the article it works.
LikeLiked by 1 person
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
LikeLike
I just get a flashing cursor after running the script
LikeLike
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
LikeLike
Hi,
$result is a “kind” of an array, it’s called a hashtable.
In $result, I’m collecting all that comes from the Try and Catch Block.
Best,
P
LikeLike
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.
LikeLike
Hi Daniel,
Thank you for your comment and hint. That’s correct. This function is for one domain only.
All the best,
P
LikeLike