PowerShell to View System Uptime

Learn how to use PowerShell to view system uptime in this guest post by Jordan Krause, a six-time Microsoft MVP and the author of Windows Server 2016 Administration Cookbook.

It often happens that you find yourself constantly checking servers to figure out what time they last restarted. Usually, this is done as part of troubleshooting something in order to figure out whether the server rebooted as a planned action or if something went wrong.

Earlier, you had to launch Event Viewer, wait for the System logs to open, and calculate the number of seconds that the system was online. Way too complicated! Thankfully, you can now make calls to WMI objects with PowerShell; it has an object that will tell you the last time the server started. With a few lines plugged into a .ps1 script, you can create a nice little tool that will output the last time that a server booted. Time to try it out!

How to do it…

To build a script that shows the last system boot time, perform the following steps:

  1. Launch PowerShell ISE as an Administrator.
  2. Open up a new script file and input the following line:

     Get-WmiObject -Class Win32_OperatingSystem -ComputerName 

      localhost | Select-Object -Property LastBootUpTime

PowerShell

  1. You have some data! It’s kind of messy, though. Perhaps you can clean that up and make it a little more readable. With a couple of changes to the Select-Object code, you can change the header for this data and the output of the date and time to make it easier on the eyes:

Get-WmiObject -Class Win32_OperatingSystem -ComputerName

       localhost | Select-Object -Property @{n=”Last Boot Time”;e=    

       {[Management.ManagementDateTimeConverter]::ToDateTime

       ($_.LastBootUpTime)}}

PowerShell 2

This looks much better. At this point, the script is ready to be saved and used on any individual machine, and it would quickly give you the output you are looking for on that particular server. But, as you can see in the code, the computer name has been hardcoded to be localhost, the server or computer where the script is currently running. What if you could change the computer name so that the user running this script could enter a different computer name? Maybe you could then use this script to execute a remote reach and find out when different servers last booted, without having to log into those servers?

Here’s an example. With a few changes to the code, you can require the user to input a computer name as a flag while running the script, and output two properties. Place an additional property identifier in there for the computer name itself so that it is clear in the output.

  1. Use this for your script code:

PowerShell 3

  1. Now when you run this script, you are asked to input the server name that you want to query:

PowerShell 4

  1. Go ahead and type localhost, and you will receive the same boot time information as before, but now you can see a new column that shows the server’s name as well:

PowerShell 5

  1. Try running the script again, but enter the name of a remote server this time when it asks for ServerName. You should now see the last boot time output for that particular server with its name in the left column:

How it works…

In this tutorial, you created a fun little script that asks for a server name and outputs the last boot information for the server entered. One of PowerShell’s greatest attributes is its ability to grab information, both locally and on remote machines. This saves time, since you don’t have to log into those servers to accomplish tasks, and makes your work environment more efficient.

One note on particular script that was created: you don’t have to run it as a first step and then take a second step in order to enter the server name. You can place the ServerName variable into your initial command when you launch the script. For example, open PowerShell and input the following command to launch the script:

.’Check Boot Time.ps1′ -ServerName DC1

This will launch the script and automatically input DC1 as the server that it is checking, instead of stopping to ask you for the input.

If you found this tutorial helpful, you can always refer to the book, Windows Server 2016 Administration Cookbook, by Jordan Krause. Packed with numerous hands-on recipes, this book is a must-have for system administrators and IT professionals who want to manage the core infrastructure of Windows Server 2016 environment.

Hope that this is useful information for you.

peace with you,

Dautti

LinkedIn | YouTube | Twitter | Instagram | Facebook

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.