PoshCode Archive  Artifact [e74461a406]

Artifact e74461a406b2899790dacd86cac42bd936181c6ed069cda0755a0800de3c969d:

  • File Get-Uptime.ps1 — part of check-in [464232cd09] at 2018-06-10 13:37:57 on branch trunk — Get the system uptime of the localhost or a remote host. (user: Tony Sathre size: 1280)

# 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'
    }
}