Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[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
You can’t perform that action at this time.