Skip to content

Instantly share code, notes, and snippets.

@dennisrhodes
Created January 12, 2016 08:29
Show Gist options
  • Save dennisrhodes/b3c611a694161d3ce7df to your computer and use it in GitHub Desktop.
Save dennisrhodes/b3c611a694161d3ce7df to your computer and use it in GitHub Desktop.
january-2016-scripting-games-puzzle
function Get-Uptime{
[CmdletBinding()]
param
(
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[String[]]$ComputerName = $env:COMPUTERNAME
)
PROCESS{
foreach($computer in $ComputerName){
$isAlive = Test-Connection -ComputerName $computer -Count 1 -Quiet
if($isAlive -eq $true){
$compSystem = Get-WmiObject -ComputerName $computer -ClassName Win32_ComputerSystem
$osSystem = Get-WmiObject -ComputerName $computer -ClassName Win32_OperatingSystem
$bool = ((Get-Date)-$osSystem.ConvertToDateTime($osSystem.LastBootUpTime)).Days -gt 30
$hash = [ordered]@{
ComputerName = $compSystem.Name
StartTime = $osSystem.ConvertToDateTime($osSystem.LastBootUpTime)
"Uptime (Days)" = ((Get-Date)-$osSystem.ConvertToDateTime($osSystem.LastBootUpTime)).Days
Status = "OK"
MightNeedPatched = $bool
}
$obj = New-Object -TypeName psobject -Property $hash
$obj
}
else{
$hash = [ordered]@{
ComputerName = $computer
StartTime = ""
"Uptime (Days)" = ""
Status = "ERROR"
MightNeedPatched = ""
}
Write-Warning -Message "$computer is Offline"
$obj = New-Object -TypeName psobject -Property $hash
$obj
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment