Using Powershell to get your server’s uptime!

I have always missed the option in windows to just easily see the uptime from a server.

Ofcourse we have the uptime.exe available from Microsoft, but I wanted to have something available directly from powershell.

For starters we could get the last bootuptime from WMI with the following query

(Get-WmiObject -Class win32_operatingsystem).lastbootuptime
20151005150834.490241+120

Since this is not really in a nice format, lets change it into something we can read

[System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject -Class win32_operatingsystem).lastbootuptime)
Tuesday, October 6, 2015 10:37:14 AM

Now that we have the bootup time/date in a readable format we can substract this date from our current time/date to get our actual uptime.

((Get-Date)-([System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject -Class win32_operatingsystem).lastbootuptime)))

Days              : 0
Hours             : 0
Minutes           : 16
Seconds           : 8
Milliseconds      : 736
Ticks             : 9687365862
TotalDays         : 0.0112122290069444
TotalHours        : 0.269093496166667
TotalMinutes      : 16.14560977
TotalSeconds      : 968.7365862
TotalMilliseconds : 968736.5862

And there you go, you can now see the uptime for your server. I took this along a bit and created a powershell function to make it more usable for everyone in the company.

Function Get-Uptime 
{
    <#
            .SYNOPSIS
            Get uptime for Server(s)
            .DESCRIPTION
            This script will provide you the uptime for your servers in easy format
            .PARAMETER Servers
            One or multiple servers
            .EXAMPLE
            Get-Uptime -Server "computername"
            Request the uptime for a server
    #>
    [CmdletBinding(SupportsShouldProcess = $true)]

    param(
        [parameter(Mandatory = $true, HelpMessage = 'Please enter a computername.')]
        [alias('Computernames','Computername','Server','Computer')]
        [string[]]$servers
    )

    ForEach ($server in $servers) 
    {
        Write-Verbose -Message "Retrieving information from $server"
        $uptime = ((Get-Date )-([System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject -Class win32_operatingsystem -ComputerName $server -ErrorAction SilentlyContinue ).lastbootuptime)))
        $server | Select-Object -Property @{
            Name       = 'Computername'
            Expression = {
                $server
            }
        }, 
        @{
            Name       = 'Days'
            Expression = {
                $uptime.Days
            }
        }, 
        @{
            Name       = 'Hours'
            Expression = {
                $uptime.Hours
            }
        }, 
        @{
            Name       = 'Minutes'
            Expression = {
                $uptime.Minutes
            }
        }, 
        @{
            Name       = 'Seconds'
            Expression = {
                $uptime.Seconds
            }
        }, 
        @{
            Name       = 'Milliseconds'
            Expression = {
                $uptime.Milliseconds
            }
        }
    }
}

Now you only need to type Get-Uptime “servername” to get the uptime for the server you wish 🙂

PS C:\> get-uptime localhost | FT

Computername Days Hours Minutes Seconds Milliseconds
------------ ---- ----- ------- ------- ------------
localhost       0    19      49      32          478

 

Leave a Reply

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