Skip to content

Instantly share code, notes, and snippets.

@sellynx sellynx/get-uptime.ps1
Last active Feb 4, 2016

Embed
What would you like to do?
2016-January Scripting Games Puzzle
function get-uptime{
param(
[Parameter(Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
[string[]]$computername=$env:COMPUTERNAME
)
$manyObj =@()
foreach($computer in $computername){
$itemObj = New-Object System.Object
$itemObj | Add-Member -type NoteProperty -name ComputerName $computer
if (Test-Connection $computer -Count 1 2> NULL){
try{
$bootup=(Get-WmiObject -Class Win32_OperatingSystem -computername $computer)
$lastboot = $bootup.ConvertToDateTime($bootup.LastBootUpTime)
$days = [Math]::Round((((Get-Date) - $lastboot).TotalDays), 1)
$itemObj | Add-Member -type NoteProperty -name StartTime $lastboot
$itemObj | Add-Member -type NoteProperty -name "Uptime (Days)" $days
$itemObj | Add-Member -type NoteProperty -name Status "OK"
if ($days -gt 30) { $itemObj | Add-Member -type NoteProperty -name MightNeedPatched $true}
else { $itemObj | Add-Member -type NoteProperty -name MightNeedPatched $false}
$manyObj +=$itemObj
}
catch {
$itemObj | Add-Member -type NoteProperty -name StartTime ""
$itemObj | Add-Member -type NoteProperty -name "Uptime (Days)" ""
$itemObj | Add-Member -type NoteProperty -name Status "ERROR"
$manyObj +=$itemObj}
}
else
{
#status
$itemObj | Add-Member -type NoteProperty -name StartTime ""
$itemObj | Add-Member -type NoteProperty -name "Uptime (Days)" ""
$itemObj | Add-Member -type NoteProperty -name Status "Offline"
$manyObj +=$itemObj
}
}
$manyObj| ft
}
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.