Skip to content

Instantly share code, notes, and snippets.

@githubbery githubbery/Get-Uptime.ps1
Last active Jan 8, 2016

Embed
What would you like to do?
function Get-Uptime {
Param (
[parameter(ValueFromPipelineByPropertyName)]
[Alias('name')]
[string[]]$Computername
)
$today = Get-Date
$array = @()
$online = @()
$offline = @()
foreach ($server in $Computername)
{
$ping = Test-Connection -ComputerName $server -Count 1 -Quiet
if ($ping -eq $true) { $online += $server }
else { $offline += $server }
}
if ($computername)
{
foreach ($onlineServer in $online)
{
$tempArray = Get-CimInstance -ClassName win32_operatingsystem -computername $onlineServer -ErrorAction SilentlyContinue | select `
@{label="ComputerName";expression={$_.PSComputerName}},`
@{label="StartTime";expression={$_.LastBootUpTime}},`
@{label="Uptime (Days)";expression={(New-TimeSpan -Start $_.LastBootUpTime.ToShortDateString() -End $today.ToShortDateString()).Days}},`
@{label="Status";expression={$_.Status}}
}
if ($tempArray.StartTime -eq $null)
{
$temp = @{}
$temp.ComputerName = $onlineServer
$temp.StartTime = "-"
$temp."Uptime (Days)" = "-"
$temp.Status = "ERROR"
$tempObject = New-Object PSobject -Property $temp
$array += $tempObject
}
else {$array += $tempArray}
foreach ($serverName in $offline)
{
$offlineArray = @{}
$offlineArray.ComputerName = $serverName
$offlineArray.StartTime = "-"
$offlineArray."Uptime (Days)" = "-"
$offlineArray.Status = "OFFLINE"
$offlineObject = New-Object PSobject -Property $offlineArray
$array += $offlineObject
}
}
else
{
$tempArray = Get-CimInstance -ClassName win32_operatingsystem -computername $env:COMPUTERNAME | select `
@{label="ComputerName";expression={$_.PSComputerName}},`
@{label="StartTime";expression={$_.LastBootUpTime}},`
@{label="Uptime (Days)";expression={(New-TimeSpan -Start $_.LastBootUpTime.ToShortDateString() -End $today.ToShortDateString()).Days}}
@{label="Status";expression={$_.Status}}
if ($tempArray.StartTime -eq $null)
{
$temp = @{}
$temp.ComputerName = $onlineServer
$temp.StartTime = "-"
$temp."Uptime (Days)" = "-"
$temp.Status = "ERROR"
$tempObject = New-Object PSobject -Property $temp
$array += $tempObject
}
else {$array += $tempArray}
}
foreach ($row in $array)
{
if ($row."Uptime (Days)" -eq "-") {$value = "-"}
elseif ($row."Uptime (Days)" -ge 30) {$value = "True"}
elseif ($row."Uptime (Days)" -lt 30) {$value = "False"}
$row | Add-Member -MemberType NoteProperty -Name MightNeedPatched -Value ($value)
}
$array | ft ComputerName,StartTime,"Uptime (Days)",Status,MightNeedPatched -AutoSize
}
 
Get-Uptime -Computername #comma sperated computer names here
@githubbery

This comment has been minimized.

Copy link
Owner Author

githubbery commented Jan 8, 2016

Get-Uptime function; January PowerShell.org Scripting Games.
7 Requirements + Bonus.

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.