Skip to content

Instantly share code, notes, and snippets.

@KirillPashkov
Last active January 5, 2016 19:18
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 KirillPashkov/a979a2f24261be898ba7 to your computer and use it in GitHub Desktop.
Save KirillPashkov/a979a2f24261be898ba7 to your computer and use it in GitHub Desktop.
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