Skip to content

Instantly share code, notes, and snippets.

@BennettBenson
Created January 15, 2016 23:52
Show Gist options
  • Save BennettBenson/5ea19b7d72d12bef5786 to your computer and use it in GitHub Desktop.
Save BennettBenson/5ea19b7d72d12bef5786 to your computer and use it in GitHub Desktop.
function Get-Uptime {
param ($snames = 'localhost')
$global:objects = @()
foreach ($sname in $snames) {
$object = New-Object –TypeName PSObject
$object | Add-Member –Type NoteProperty –Name Name –Value $sname
Try {
Test-Connection $sname -ErrorAction Stop | Out-Null
Try {
($osinfo = Get-CimInstance -ClassName win32_operatingsystem -ComputerName $sname -ErrorAction Stop) > $null
$object | Add-Member –Type NoteProperty –Name Status –Value 'OK'
$object | Add-Member –Type NoteProperty –Name Boottime –Value $(NEW-TIMESPAN -start $($osinfo.lastbootuptime))
$object | Add-Member –Type NoteProperty –Name UptimeInDays –Value $([math]::round($uptime.TotalDays,1))
if ($([math]::round($uptime.TotalDays,1)) -ge 30) {
$object | Add-Member –Type NoteProperty –Name Needspatching –Value $true
}
else{
$object | Add-Member –Type NoteProperty –Name Needspatching –Value $false
}
}
Catch {
#$status = 'Error'
$object | Add-Member –Type NoteProperty –Name Status –Value 'Error'
}
}
Catch {
Write-Warning "$sname is offline"
$object | Add-Member –Type NoteProperty –Name Status –Value 'Offline'
}
$global:objects += $object
}
$objects | Format-Table
}
$args = $null
$args = @()
$arg1 = '127.0.0.1'
$arg2 = 'localhost'
$arg3 = 'DESKTOP-80JCHGC'
$arg4 = 'abc-123'
$arg5 = '192.168.1.139'
$args += $arg1
$args += $arg2
$args += $arg3
$args += $arg4
$args += $arg5
Get-Uptime $args
<#Requirements:
1. Support pipeline input so that you can pipe computer names directly to it.
2. Process multiple computer names at once time and output each computer’s stats with each one being a single object.
3. It should not try to query computers that are offline.
If an offline computer is found, it should write a warning to the console yet still output an object but with Status of OFFLINE.
4. If the function is not able to find the uptime it should show ERROR in the Status field.
5. If the function is able to get the uptime, it should show ‘OK’ in the Status field.
6. It should include the time the server started up and the uptime in days (rounded to 1/10 of a day)
7. If no ComputerName is passed, it should default to the local computer.
Bonus:
1. The function should show a MightNeedPatched property of $true ONLY if it has been up for more than 30 days (rounded to 1/10 of a month). If it has been up for less than 30 days, MightNeedPatched should be $false.
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment