# encoding: ascii
# api: powershell
# title: Get-Uptime
# description: Get the system uptime of the localhost or a remote host.
# version: 0.1
# type: class
# author: Tony Sathre
# license: CC0
# x-poshcode-id: 4190
# x-archived: 2013-06-13T05:48:32
# x-published: 2013-05-30T20:35:00
#
#
Param (
[string]$computerName = $env:computerName
)
if ($computerName -eq $env:computerName) {
$os = Get-WmiObject -Class win32_operatingsystem
$boottime = [management.managementDateTimeConverter]::ToDateTime($os.LastBootUpTime)
$now = [DateTime]::Now
$uptime = New-TimeSpan -Start $boottime -End $now
Write-Host "Current System Uptime on $computerName`:" $uptime.Days 'Days,' $uptime.Hours 'Hours,' $uptime.Minutes 'Minutes,' $uptime.Seconds 'Seconds'
} else {
Invoke-Command -ComputerName $computerName -ScriptBlock {
$os = Get-WmiObject -Class win32_operatingsystem
$boottime = [management.managementDateTimeConverter]::ToDateTime($os.LastBootUpTime)
$now = [DateTime]::Now
$uptime = New-TimeSpan -Start $boottime -End $now
Write-Host "Current System Uptime on $env:computerName`:" $uptime.Days 'Days,' $uptime.Hours 'Hours,' $uptime.Minutes 'Minutes,' $uptime.Seconds 'Seconds'
}
}