Last active
April 29, 2016 11:56
-
-
Save XPlantefeve/36e544cd40f39b4d4d55 to your computer and use it in GitHub Desktop.
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
<# | |
.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