Skip to content

Instantly share code, notes, and snippets.

@bundyfx
Last active January 3, 2016 10:05
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 bundyfx/f84c6716e743c0a488c0 to your computer and use it in GitHub Desktop.
Save bundyfx/f84c6716e743c0a488c0 to your computer and use it in GitHub Desktop.
Jan 2016 Scripting Games
<#
.Synopsis
Get the Up time (Last boot time) of target machines.
.EXAMPLE
Get-Uptime -Computername DC1,NLB1,NLB2
.EXAMPLE
$Collection | Gup
.EXAMPLE
$Collection | Get-Uptime | Export-CSV C:\Temp\UpTime.csv -nti
.EXAMPLE
Get-Uptime -ComputerName DC1,NLB1,NLB2 | Format-Table -AutoSize
#>
function Get-Uptime
{
[CmdletBinding()]
[Alias('gup')]
Param
(
[AllowNull()]
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
ValueFromPipeline=$true,
Position=0)]
[String[]]
$ComputerName
)
Begin
{
#if no computername passed in by user, use Localhost for the Computername Variable
if (!($PSBoundParameters.ContainsKey('ComputerName'))){$ComputerName = "$Env:COMPUTERNAME"}
}
Process
{
foreach ($Computer in $ComputerName){
if (Test-Connection $Computer -Count 1 -Quiet){
#Gather last boot up time from WMI
$Basetime = Get-WmiObject -ComputerName $Computer -Class WIN32_Operatingsystem
#Create Custom object with the information we need
$obj = "" | Select @{Name='ComputerName'; Expression={$Basetime.PSComputerName}},
@{Name='Start Time'; Expression={$Basetime.ConverttoDateTime($Basetime.LastBootUpTime)}},
@{Name='Uptime (Days)'; Expression={[int][math]::round((New-TimeSpan -Start ($Basetime.ConverttoDateTime($Basetime.LastBootUpTime)) -End (Get-date)).totaldays ,2.4)}},
@{Name='Status'; Expression={if ($Basetime.LastBootUpTime -eq $Null){return 'ERROR'}else {return 'OK'}}},
@{Name='MightNeedPatches';Expression={if ([int][math]::round((New-TimeSpan -Start ($Basetime.ConverttoDateTime($Basetime.LastBootUpTime)) -End (Get-date)).totaldays ,2.4) -gt 30){return $true}else{return $false}}}
} else {Write-Warning "$Computer is NOT online";$obj = "" | Select @{Name='ComputerName';Expression={$Computer}},@{Name = 'Status';Expression = {return "OFFLINE"}}}
[Array]$ObjArray += $Obj
}
} #End of Process
End
{
#display obj
$ObjArray
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment