Last active
January 11, 2016 04:37
-
-
Save kvprasoon/8ba60788ccc84bc535c8 to your computer and use it in GitHub Desktop.
ScriptingGamesJan2016
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(ValueFromPipeline = $true)] | |
[String[]]$ComputerName = $env:COMPUTERNAME | |
) | |
Begin | |
{ | |
$Output=@() | |
Function CreateObject | |
{ | |
param( | |
[String]$CSname, | |
$LBUtime, | |
$LCTime , | |
[String]$Status, | |
[bool]$Patching = $false | |
) | |
if($Status -eq 'offline'){ $Uptime = $LBUtime = 'Not Available' } | |
elseif($LBUtime -ne $null) | |
{ | |
$day = "{0:0.00}" -f ( $LCTime - $LBUtime ).TotalDays | |
if ( ($LCTime - $LBUtime).TotalDays -like '*.889' ) { $day++ }# Rounding to 1/10 th of a Day | |
$Uptime = $day | |
if( $Uptime -gt 27 ) { $Patching = $true }# Rounding to 1/10th of a month | |
} | |
else { $Status = 'Error' } | |
$Output = New-Object -TypeName psobject -Property @{ | |
'ComputerName' = $CSname | |
'StartTime' = $LBUtime | |
'Uptime (Days)' = $Uptime | |
'Status' = $Status | |
'MightNeedPatched' = $Patching | |
} | |
return $Output | Select-Object -Property ComputerName,StartTime,'Uptime (Days)',Status,MightNeedPatched | |
} | |
}# Begin Block | |
Process | |
{ | |
foreach($Computer in $ComputerName) | |
{ | |
if( Test-Connection -ComputerName $Computer -Count 2 -Quiet ) | |
{ | |
Write-Verbose -Message "[$Computer]: querying Uptime..." -Verbose | |
$OSDetails = Get-WmiObject -ClassName Win32_OperatingSystem -ComputerName $Computer | |
$Output+= CreateObject -CSname $OSDetails.CSName -LBUtime $OSDetails.ConvertToDateTime($OSDetails.LastBootUpTime) -LCTime $OSDetails.ConvertToDateTime($OSDetails.LocalDateTime) -Status $OSDetails.Status | |
}# If Computer Online | |
else | |
{ | |
Write-Warning -Message "Computer '$Computer' is Offline or Not able to contact" | |
$Output+= CreateObject -CSname $Computer -Status 'Offline' | |
} | |
}# Foreach | |
}# Process Block | |
End | |
{ | |
$Output | |
}# End Block | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment