# 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: 4201
# x-archived: 2013-11-27T06:30:59
# x-published: 2013-06-13T02:46:00
#
# Very nice little script.  Here is a modification that uses WMI remote invocation instead of PS remoting.  In my experience, this is more likely to succeed.
#
Param (
    [string]$computerName = $env:computerName
)

$os = Get-WmiObject -ComputerName $computerName -Class win32_operatingsystem
$boottime = [management.managementDateTimeConverter]::ToDateTime($os.LastBootUpTime)
$now = [management.managementDateTimeConverter]::ToDateTime($os.localdatetime)
$uptime = New-TimeSpan -Start $boottime -End $now
if ($computername -ne $os.csname) { $altname = " ($($os.csname))" }
Write-Host "Uptime on ${computerName}${altname}`:" $uptime.Days 'Days,' $uptime.Hours 'Hours,' $uptime.Minutes 'Minutes,' $uptime.Seconds 'Seconds'
