Created
January 8, 2016 16:33
-
-
Save jeffbuenting/5f39afcc19f62ed5fb91 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
Function Get-Uptime { | |
<# | |
.Synopsis | |
Gets a computer's uptime information. | |
.Description | |
Gets a computers uptime information including: | |
Uptime in Days | |
Starttime | |
Status | |
if it may need to be patched | |
.Parameter ComputerName | |
Name of the computer from which to get the uptime. | |
.Example | |
Get-Uptime -ComputerName ServerA | |
.Link | |
http://powershell.org/wp/2016/01/02/january-2016-scripting-games-puzzle/ | |
.Notes | |
Author: Jeff Buenting | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter(ValuefromPipeline=$True)] | |
[String[]]$ComputerName = $env:COMPUTERNAME | |
) | |
Process { | |
foreach ( $C in $ComputerName ) { | |
Write-Verbose "Checking uptime for $C" | |
$Uptime = New-Object -TypeName psobject -Property @{ | |
ComputerName = $C | |
StartTime = $Null | |
'Uptime (Days)' = $Null | |
Status = 'OK' | |
MightNeedPatched = $Null | |
} | |
if ( Test-Connection -ComputerName $C -Quiet ) { | |
# ----- Computer is reachable | |
$OS = Get-CimInstance -ComputerName $C -ClassName Win32_OperatingSystem | |
Try { | |
$U = $OS.LocalDateTime – $OS.LastBootUpTime | |
# ----- Check if $U has a value. If not then cannot find uptime | |
if ( $U -eq $Null ) { | |
Write-Verbose "Cannot Find Uptime" | |
$Uptime.Status = 'ERROR' | |
} | |
Else { | |
$Uptime.StartTime = $OS.LastBootUpTime | |
$Uptime.'Uptime (Days)' = "{0:N1}" -f $U.TotalDays | |
$Uptime.MightNeedPatched = ( [Int]$Uptime.'Uptime (Days)' -gt 30 ) | |
} | |
} | |
Catch { | |
# ----- Any error generated from the uptime calculation will end up here | |
Write-Verbose "Cannot Find Uptime" | |
$Uptime.Status = 'ERROR' | |
} | |
} | |
Else { | |
Write-Warning "$C is OFFLINE" | |
$Uptime.Status = 'OFFLINE' | |
} | |
Write-Output $Uptime | |
} | |
} | |
} |
Also, if you name your Gist with a .ps1 extension, it will automatically include syntax highlighting.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quick tip: you can speed up your ping check (Test-Connection) by adding the parameter "-Count 1" to it, so it will only attempt a singe ping; by default, it pings 4 times with a short delay between each ping.