I am having trouble with the output of my script displaying information in the way that I would like it to. I would like to view the matching properties (objects are generated in a foreach loop, so they will always have the exact same properties) of each generated custom object under one table, instead of creating a new table for each object like the script below currently does. Is this possible with the way this script creates the object? If so, how would it be done?

function Get-Uptime
    [String[]]$ComputerName = $env:COMPUTERNAME

Begin {}
Process {
   foreach ($computer in $ComputerName) {
       $ComputerObject = [Ordered]@{"Computer Name" = $computer
                                    "Start Time" = $null
                                    "Uptime (Days)" = $null
                                    "Status" = $null
                                    "May Need Patched?" = $null}
       $IsOnline = Test-Connection -ComputerName $Computer -Quiet
       if ($IsOnline = $true) {
           Try { 
           #Collect information about each computer
           $WMIObject = Get-WMIObject -Class Win32_OperatingSystem -ComputerName $computer
           #Convert LastBootUpTime to readable date/time
           $ComputerObject.'Start Time' = $WMIObject.ConvertToDateTime($WMIObject.LastBootUpTime)
           #Convert LocalDateTime to readable date/time
           $ComputerObject.Uptime = $WMIObject.ConvertToDateTime($WMIObject.LocalDateTime)
           #Collect the number of days that have elapsed between
           $TimeElapsed = New-TimeSpan -Start $ConvertedBootUpTime -End $ConvertedLocalTime
           $ComputerObject.'Uptime (Days)' = [Math]::Round($TimeElapsed.TotalDays,1) 
           $ComputerObject.'May Need Patched?' = If ($ComputerObject.'Uptime (Days)' -ge "30"){$True} Else{$False}
           $ComputerObject.Status = "Online"
           Catch {
           Write-Error "Unable to gather uptime for $Computer"
           $ComputerObject.Status = "Error"
       else {
           Write-Error "Computer $Computer is either non-existant or offline"
           $ComputerObject.Status = "Offline"
       [PSCustomObject]$ComputerObject | ft
End {}

Problem is you're calling Format-Table on each object as you're displaying them. Remove that and you're set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.