Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Function Get-Uptime {
[CmdletBinding()]
param (
[parameter(ValueFromPipeline=$true,ValueFromPipeLineByPropertyName=$true,Position=0)]
[alias("PScomputername")]
[string[]]$ComputerName='localhost'
)
Begin {
$results = @()
$ISS = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
$RunspacePool = [runspacefactory]::CreateRunspacePool(1, 75, $ISS, $Host)
$RunspacePool.Open()
$Jobs = @()
$Scriptblock = {
param (
$computer
)
if (Test-Connection -ComputerName $computer -quiet -Count 1) {
try {
$OS = Get-WmiObject -ComputerName $computer -Class Win32_OperatingSystem
$lastboottime = $OS.ConvertToDateTime($OS.LastBootUpTime)
$uptime = [Math]::Round(((Get-Date) - $lastboottime).totaldays)
if ($uptime -ge 30) {$mightneedpatched = $true } else {$mightneedpatched = $false}
$status = "OK"
} catch {
$return = New-Object -TypeName PSCustomObject
$Return | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $computer
$return | Add-Member -MemberType NoteProperty -Name "StartTime" -Value $null
$return | Add-Member -MemberType NoteProperty -Name "Uptime(Days)" -Value $null
$return | Add-Member -MemberType NoteProperty -Name "MightNeedPatched" -Value $null
$Return | Add-Member -MemberType NoteProperty -Name "Status" -Value "ERROR"
return $return
}
$return = New-Object -TypeName PSCustomObject
$Return | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $computer
$return | Add-Member -MemberType NoteProperty -Name "StartTime" -Value $lastboottime
$return | Add-Member -MemberType NoteProperty -Name "Uptime(Days)" -Value $uptime
$return | Add-Member -MemberType NoteProperty -Name "MightNeedPatched" -Value $mightneedpatched
$Return | Add-Member -MemberType NoteProperty -Name "Status" -Value $status
return $return
} else {
Write-Warning "$computer is offline"
$return = New-Object -TypeName PSCustomObject
$Return | Add-Member -MemberType NoteProperty -Name ComputerName -Value $computer
$return | Add-Member -MemberType NoteProperty -Name "StartTime" -Value $null
$return | Add-Member -MemberType NoteProperty -Name "Uptime(Days)" -Value $null
$return | Add-Member -MemberType NoteProperty -Name "MightNeedPatched" -Value $null
$Return | Add-Member -MemberType NoteProperty -Name Status -Value "OFFLINE"
return $return
}
}
}
Process {
foreach ($computer in $computername) {
$PowershellThread = [powershell]::Create().AddScript($scriptblock)
$PowershellThread.AddParameter("computer", $computer) | out-null
$PowershellThread.RunspacePool = $RunspacePool
$Handle = $PowershellThread.BeginInvoke()
$Job = "" | Select-Object Handle, Thread, object
$Job.Handle = $Handle
$Job.Thread = $PowershellThread
$Job.Object = $computer.ToString()
$Jobs += $Job
}
}
End {
While (@($Jobs | Where-Object {$_.Handle -ne $Null}).count -gt 0) {
Write-Progress -Activity "Waiting for Jobs - $(75 - $($RunspacePool.GetAvailableRunspaces())) of 75 runspaces running" `
-PercentComplete (($Jobs.count - $($($Jobs | Where-Object {$_.Handle.IsCompleted -eq $False}).count)) / $Jobs.Count * 100) `
-Status "$(@($($Jobs | Where-Object {$_.Handle.IsCompleted -eq $False})).count) remaining - $($($Jobs | Where-Object {$_.Handle.IsCompleted -eq $False}).object)"
ForEach ($Job in $($Jobs | Where-Object {$_.Handle.IsCompleted -eq $True})){
$Job.Thread.EndInvoke($Job.Handle)
$Job.Thread.Dispose()
$Job.Thread = $Null
$Job.Handle = $Null
$ResultTimer = Get-Date
}
}
$RunspacePool.Close() | Out-Null
$RunspacePool.Dispose() | Out-Null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment