PoshCode Archive  Artifact [6607a31b19]

Artifact 6607a31b192bc1df8fe83deb304694d1e2f150c4d434ab4312c181a706b1e875:

  • File Get-Uptime.ps1 — part of check-in [704cd963e2] at 2018-06-10 13:38:12 on branch trunk — Get the system uptime of the localhost or a remote host. (user: Tony Sathre size: 1035)

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