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.
To build a script that shows us the last system boot time, perform the following steps:
Get-WmiObject -Class Win32_OperatingSystem -ComputerName
localhost | Select-Object -Property LastBootUpTime
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)}}
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.
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)}}
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.
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.
3.14.130.230