Skip to content

Instantly share code, notes, and snippets.

@DezCorps
Last active January 15, 2016 20:36
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 DezCorps/33b97c66972b9b2fcacc to your computer and use it in GitHub Desktop.
Save DezCorps/33b97c66972b9b2fcacc to your computer and use it in GitHub Desktop.
Get-Uptime is a cmdlet to gather the uptime for Windows Systems around the network. You can get a single or multiple nodes StartTime, Uptime in days, Status and if it needs to be patched.
<#
.Synopsis
Cmdlet for checking the uptime for a Windows System.
.DESCRIPTION
Get-Uptime is a cmdlet to gather the uptime for Windows Systems around the network.
You can get a single or multiple nodes StartTime, Uptime in days, Status and if it needs to be patched.
.EXAMPLE
Get-Uptime -ComputerName IT-GSD-01
ComputerName StartTime Uptime(Days) Status MightNeedPatched
------------ --------- ------------ ------ ----------------
IT-GSD-01 01/04/2016 07:30:30 11 OK False
.EXAMPLE
Get-Uptime -ComputerName IT-GSD-01, SMS-LOC-03, PSM-LOC-01, PDDKKSL
ComputerName StartTime Uptime(Days) Status MightNeedPatched
------------ --------- ------------ ------ ----------------
IT-GSD-01 01/04/2016 07:30:30 11 OK False
SMS-LOC-03 12/28/2015 06:10:49 18 OK False
PSM-LOC-01 ERROR ERROR OK False
PDDKKSL OFFLINE OFFLINE
#>
Function Get-Uptime
{
[CmdletBinding()]
Param
(
# Param help description
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string[]]$ComputerName
)
Begin
{
$Output = @()
}
Process
{
Foreach($Server in $ComputerName)
{
If($strConnection = Test-Connection -ComputerName $Server -Count 1 -Quiet)
{
Try{
$strOS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Server -ErrorAction SilentlyContinue
$strUptime = (New-TimeSpan $($strOS.ConvertToDateTime($strOS.LastBootUpTime)) $(Get-Date)).Days
$strLastBoot = $strOS.ConvertToDateTime($strOS.LastBootUpTime)
$strStatus = "OK"
If($strUptime -ge 30)
{
$strPatched = "$True"
}
ElseIf($strUptime -lt 30)
{
$strPatched = "$False"
}
}
Catch{
$strLastBoot = "ERROR"
$strUptime = "ERROR"
$strStatus = "OK"
$strPatched = "$False"
}
$object = New-Object PSObject
$object | Add-Member –MemberType NoteProperty –Name 'ComputerName' –Value "$($Server)"
$object | Add-Member –MemberType NoteProperty –Name 'StartTime' –Value "$($strLastBoot)"
$object | Add-Member –MemberType NoteProperty –Name 'Uptime(Days)' –Value " $(($strUptime))"
$object | Add-Member –MemberType NoteProperty –Name 'Status' –Value "$($strStatus)"
$object | Add-Member –MemberType NoteProperty –Name 'MightNeedPatched' –Value "$($strPatched)"
$Output += $object
}
Else
{
$object = New-Object PSObject
$object | Add-Member –MemberType NoteProperty –Name 'ComputerName' –Value "$($Server)"
$object | Add-Member –MemberType NoteProperty –Name 'StartTime' –Value "OFFLINE"
# $object | Add-Member –MemberType NoteProperty –Name 'Uptime(Days)' –Value " - "
$object | Add-Member –MemberType NoteProperty –Name 'Status' –Value "OFFLINE"
# $object | Add-Member –MemberType NoteProperty –Name 'MightNeedPatched' –Value " - "
$Output += $object
}
$Server = ""
$strOS = ""
$strLastBoot = ""
$strUptime = ""
$strStatus = ""
$strPatched = ""
}
Write-Output $Output | FT -AutoSize
}
End
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment