The below works and writes the output to the console. I can’t figure out how to sort the results on LastBoot once it completes.
I tried $Obj | sort LastBoot
Also tried $report += $object then $report | sort LastBoot
Function Get-Uptime {
[CmdletBinding()]
Param (
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName,
[Switch]$ErrorLog,
[string]$Logfile = 'c:\it\errorlog.txt',
[Switch]$OU
)
if ($ou.IsPresent) {
$oulist = Get-ADOrganizationalUnit -filter * | select DistinguishedName | Out-GridView -PassThru | Select-Object -ExpandProperty DistinguishedName
$computers = Get-ADComputer -filter * -Properties * -SearchBase $oulist | select name -ExpandProperty name
Foreach ($Computer in $computers) {
Try{
$OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer -ErrorAction Stop -ErrorVariable CurrentError
$Uptime = (Get-Date) - $OS.ConvertToDateTime($OS.LastBootUpTime)
$Properties = @{ComputerName = $Computer
LastBoot = $OS.ConvertToDateTime($OS.LastBootUpTime)
Uptime = ([String]$Uptime.Days + " Days " + $Uptime.Hours + " Hours " + $Uptime.Minutes + " Minutes")
}
$Obj = New-Object -TypeName PSObject -Property $Properties | Select ComputerName, LastBoot, UpTime
$obj
}
catch{
Write-Warning "An error occured on $Computer"
if ($ErrorLog) {
Get-Date | Out-File $LogFile -Append
$Computer | Out-File $LogFile -Append
$CurrentError | out-file $LogFile -Append
}
}
}
}
}