Last active
January 5, 2016 19:18
-
-
Save KirillPashkov/a979a2f24261be898ba7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-Uptime { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)][string[]]$ComputerName=$env:COMPUTERNAME | |
) | |
begin | |
{} | |
process { | |
$obj = New-Object PSObject | |
$obj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $ComputerName[0] | |
$connection = Test-Connection $ComputerName -Quiet -Count 1 | |
if ($connection) | |
{ | |
$wmi = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName | |
$bootup = $wmi.ConvertToDateTime($wmi.LastBootUpTime) | |
$uptime = [System.Math]::Round((New-TimeSpan -Start $bootup -End (Get-Date)).TotalDays,1) | |
$status = 'OK' | |
if ($bootup -eq $null){$status = 'ERROR'} | |
if ($uptime -ge 30){$update = $true}else{$update = $false} | |
$obj | Add-Member -MemberType NoteProperty -Name StartTime -Value $bootup | |
$obj | Add-Member -MemberType NoteProperty -Name 'Uptime (Days)' -Value $uptime | |
$obj | Add-Member -MemberType NoteProperty -Name Status -Value 'OK' | |
$obj | Add-Member -MemberType NoteProperty -Name MightNeedPatched $update | |
} | |
else | |
{ | |
@{StartTime='OFFLINE';'Uptime (Days)'='OFFLINE';Status='OFFLINE';MightNeedPatched='UNKNOWN'} | % {$obj | Add-Member $_} | |
} | |
$obj | |
} | |
end{} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment