Skip to content

Instantly share code, notes, and snippets.

@Bill-Stewart
Created June 1, 2018 20:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Bill-Stewart/c8f7e14f256a24a3974c22ea1275286b to your computer and use it in GitHub Desktop.
Save Bill-Stewart/c8f7e14f256a24a3974c22ea1275286b to your computer and use it in GitHub Desktop.
Get-Uptime.ps1 - Use WMI to get last boot and uptime
# Get-Uptime.ps1
# Written by Bill Stewart (bstewart@iname.com)
#
# Outputs uptime for one or more computers. Uptime is determined by the
# LastBootUpTime property from the Win32_OperatingSystem WMI class.
#requires -version 2
<#
.SYNOPSIS
Outputs the last bootup time and uptime for one or more computers.
.DESCRIPTION
Outputs the last bootup time and uptime for one or more computers.
.PARAMETER ComputerName
One or more computer names. The default is the current computer. Wildcards are not supported.
.PARAMETER Credential
Specifies credentials that have permission to connect to the remote computer. This parameter is ignored for the current computer.
.OUTPUTS
PSObjects containing the computer name, the last bootup time, and the uptime.
#>
[CmdletBinding()]
param(
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
$ComputerName,
[Management.Automation.PSCredential]
$Credential
)
begin {
function Out-Object {
param(
[Collections.Hashtable[]] $hashData
)
$order = @()
$result = @{}
foreach ( $item in $hashData ) {
$order += ($item.Keys -as [Array])[0]
$result += $item
}
New-Object PSObject -Property $result | Select-Object $order
}
function Format-TimeSpan {
process {
"{0} d {1:00} h {2:00} m {3:00} s" -f $_.Days,$_.Hours,$_.Minutes,$_.Seconds
}
}
function Get-Uptime {
param(
[String] $computerName,
[Management.Automation.PSCredential] $credential
)
# In case pipeline input contains ComputerName property
if ( $computerName.ComputerName ) {
$computerName = $computerName.ComputerName
}
if ( (-not $computerName) -or ($computerName -eq ".") ) {
$computerName = [Net.Dns]::GetHostName()
}
$params = @{
"Class" = "Win32_OperatingSystem"
"ComputerName" = $computerName
"Namespace" = "root\CIMV2"
}
if ( $credential ) {
# Ignore -Credential for current computer
if ( $computerName -ne [Net.Dns]::GetHostName() ) {
$params.Add("Credential", $credential)
}
}
try {
$wmiOS = Get-WmiObject @params -ErrorAction Stop
}
catch {
Write-Error -Exception (New-Object $_.Exception.GetType().FullName `
("Cannot connect to the computer '$computerName' due to the following error: '$($_.Exception.Message)'",
$_.Exception))
return
}
$lastBootTime = [Management.ManagementDateTimeConverter]::ToDateTime($wmiOS.LastBootUpTime)
Out-Object `
@{"ComputerName" = $computerName},
@{"LastBootTime" = $lastBootTime},
@{"Uptime" = (Get-Date) - $lastBootTime | Format-TimeSpan}
}
}
process {
if ( $ComputerName ) {
foreach ( $computerNameItem in $ComputerName ) {
Get-Uptime $computerNameItem $Credential
}
}
else {
Get-Uptime "."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment