Skip to content

Instantly share code, notes, and snippets.

@mmarchese
Last active January 7, 2016 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmarchese/ca100c94b5159f1d9b75 to your computer and use it in GitHub Desktop.
Save mmarchese/ca100c94b5159f1d9b75 to your computer and use it in GitHub Desktop.
[CmdletBinding()]
param (
[parameter(ValueFromPipeline=$true)]
[string[]]$ComputerName = $env:COMPUTERNAME
)
foreach ($Computer in $ComputerName) {
if (Test-Connection $Computer -Quiet -Count 1 -BufferSize 16) {
try {
$os = Get-WmiObject -Class win32_OperatingSystem -ComputerName $Computer -ErrorAction Stop
$uptime = (New-TimeSpan $($os.converttodatetime($os.lastbootuptime)) $(Get-Date)).days
$lastBootTime = $os.converttodatetime($os.lastbootuptime)
$status = "OK"
if ($uptime -ge 30) {
$MightNeedPatched = $true
}
} catch {
$uptime = "ERROR"
$lastBootTime = "ERROR"
$status = "ERROR"
$MightNeedPatched = "ERROR"
}
[PSCustomObject]@{
"Computer Name" = $Computer
"Status" = $status
"Last Booted Up" = $lastBootTime
"Uptime" = "$uptime days"
"Might Need to be Patched" = $MightNeedPatched
}
} else {
Write-Output "Computer $Computer is OFFLINE."
[PSCustomObject]@{
"Computer Name" = $Computer
"Status" = "OFFLINE"
"Last Booted Up" = "Unknown"
"Uptime" = "Unknown"
"Might Need to be Patched" = "Unknown"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment