Skip to content

Instantly share code, notes, and snippets.

@XPlantefeve XPlantefeve/Get-Uptime.ps1
Last active Apr 29, 2016

Embed
What would you like to do?
<#
.Synopsis
Gets uptime information
.EXAMPLE
Get-Uptime -ComputerName URANUS
.INPUTS
String[]
.OUTPUTS
CUSTOM.UptimeInfo
.NOTES
The MightNeedPatched property is not displayed by default.
.LINK
http://powershell.org/wp/2016/01/02/january-2016-scripting-games-puzzle/
#>
function Get-Uptime
{
[CmdletBinding()]
[OutputType([psobject])]
Param
(
# Computer(s) to query for Uptime
[Parameter(ValueFromPipeline=$true,
Position=0)]
[string[]]$ComputerName = 'localhost'
)
Begin
{
Update-TypeData -TypeName CUSTOM.UptimeInfo -DefaultDisplayPropertySet ComputerName, StartTime, 'Uptime (Days)', Status -Force
}
Process
{
foreach ( $Computer in $ComputerName ) {
if ( $Computer -eq '.' ) { $Computer = 'localhost' }
$properties = @{
ComputerName = $Computer
StartTime = $null
'Uptime (Days)' = $null
Status = $null
MightNeedPatched = $null
}
if ( Test-Connection -ComputerName $Computer -Quiet -Count 1 ) {
Try {
$wmi = gwmi -Class Win32_OperatingSystem -ComputerName $Computer
$properties.ComputerName = $wmi.PSComputerName
$Properties.StartTime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
$Properties.'Uptime (Days)' = [math]::round((( Get-Date ) - $Properties.StartTime).TotalDays,1)
$Properties.Status = 'OK'
$Properties.MightNeedPatched = ($Properties.'Uptime (Days)' -gt 30)
}
Catch {
$properties.Status = 'ERROR'
}
} else {
$properties.Status = 'OFFLINE'
}
$row = New-Object -TypeName psobject -Property $properties
$row.PSTypeNames.Insert(0,'CUSTOM.UptimeInfo')
Return $row
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.