Using PowerShell to view system uptime

I find myself constantly checking servers to figure out what time they last restarted. Usually, this is part of troubleshooting something in order to figure out whether the server rebooted as a planned action or if something went wrong and it restarted on its own during a non-standard time. For years, I had launched Event Viewer, waited for the System logs to open, hoped that they weren't corrupted in some way, and then headed over to noon on the previous day to find the number of seconds that the system had been online. Then I'd pull out the calculator and do the math for how many days/hours that really was. Way too complicated! Thankfully, we can make calls into WMI objects with PowerShell, and there is an object in there that will tell us the last time the server started. With a few lines plugged into a .ps1 script, we can create ourselves a nice little tool that will output the last time that a server booted. Let's give it a try.

Getting ready

We are using a Windows Server 2016 machine to build this script.

How to do it…

To build a script that shows us 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
    

    How to do it…

  3. We have some data! It's kind of messy data, though. Maybe we can clean that up and make it a little more readable. With a couple of changes to our Select-Object code, we can change the header for this data to something more friendly, as well as changing the output of the date and time so it's way easier on the eyes:
    Get-WmiObject -Class Win32_OperatingSystem -ComputerName
          localhost | Select-Object -Property @{n="Last Boot Time";
    e={[Management.ManagementDateTimeConverter]::ToDateTime
          ($_.LastBootUpTime)}}
    

    How to do it…

That looks much better. At this point, I would say that this 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, we are currently hardcoding the computer name to be localhost, the server or computer where we are currently running this script. What if we could change that so the user running this script could enter a different computer name? Maybe we 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 is an example of doing just that. With a few changes to our code, we can require that the user inputs a computer name as a flag while running the script, and outputs two properties now. We will place an additional property identifier in there for the computer name itself so that it is clear to us in the output that the last boot time we are looking at the server name that we actually enter.

  1. Use this for your script code:
          Param(
          [Parameter(Mandatory=$true)][string]$ServerName
          )
          Get-WmiObject -Class Win32_OperatingSystem -ComputerName
          $ServerName | Select-Object -Property CSName,@{n="Last Boot
            Time";
          e={[Management.ManagementDateTimeConverter]:: 
          ToDateTime($_.LastBootUpTime)}}
  2. Now when we run this script, we are asked to input the server name that we are trying to query.

    How to do it…

  3. Go ahead and type localhost and you will receive the same boot time information as before, but now you see that we have a new column that shows us the server's name as well.

    How to do it…

  4. Try running the script again, but this time enter the name of a remote server when it asks for ServerName. I will try to query our WEB1 web server. You should now see the last boot time output for that particular server with its name in the left column.

    How to do it…

How it works…

In this recipe, we 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 on the machine where you are running the script 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 this particular script that we 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 are able to 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 input.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.182.211