Created
May 30, 2016 13:36
-
-
Save morisy/8aa34f4ba0beaf8eef1b9224c616e041 to your computer and use it in GitHub Desktop.
Getting Computer Uptime Using PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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, | |
[System.Management.Automation.PSCredential] | |
$Credential | |
) | |
begin { | |
function Out-Object { | |
param( | |
[System.Collections.Hashtable[]] $hashData | |
) | |
$order = @() | |
$result = @{} | |
$hashData | ForEach-Object { | |
$order += ($_.Keys -as [Array])[0] | |
$result += $_ | |
} | |
New-Object PSObject -Property $result | Select-Object $order | |
} | |
function Format-TimeSpan { | |
process { | |
"{0:00} d {1:00} h {2:00} m {3:00} s" -f $_.Days,$_.Hours,$_.Minutes,$_.Seconds | |
} | |
} | |
function Get-Uptime { | |
param( | |
$computerName, | |
$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 "." | |
} | |
} |
Thanks for this. I was trying to use this to output to CSV, but I'm not having much luck. Any thoughts? My end goal is to sort my output by highest uptime to see which systems are in need of restarts.
Sort using Sort-Object cmdlet.
Get-Uptime.ps1 computer,list,here | Sort-Object Uptime -Descending
If you also need CSV output, use Export-Csv cmdlet.
Get-Uptime.ps1 computer,list,here | Sort-Object Uptime -Descending | Export-Csv -NoTypeInformation -Path "c:\temp\uptimes.csv"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this. I was trying to use this to output to CSV, but I'm not having much luck. Any thoughts? My end goal is to sort my output by highest uptime to see which systems are in need of restarts.