How do I show everything in the custom table?


How can I display the server name and uptime all in the same custom table?
How can I insert the error message in custom table?

Below is the example of the output I am attempting to achieve

Server Name		Uptime
-----------		------
ServerA			12/12/2020
ServerB			24/11/2020
ServerC			Error

This is the code

Foreach ($Server in $Serverlist)
reboot_time = Get-WmiObject win32_operatingsystem -ComputerName $Server -ErrorAction Stop | %{ $_.ConvertToDateTime($_.LastBootUpTime) }  
$result = New-Object PsObject
Add-Member -InputObject $result NoteProperty 'Server Name' $Server
Add-Member -InputObject $result NoteProperty 'Uptime' $reboot_time
$result | Select-Object "Server Name", "Uptime" | Format-Table -AutoSize


Hmm, my first Code is gone, so I try it again.
Sorry, if there is a doublePost…

Foreach ($Server in $Serverlist) {
    $reboot_time = $(Get-WmiObject win32_operatingsystem -ComputerName $Server -Errorvariable reboot_time  | ForEach-Object { $PSItem.ConvertToDateTime($PSItem.LastBootUpTime) })  2>&1
    $result = New-Object PsObject
    Add-Member -InputObject $result NoteProperty 'Server Name' $Server
    Add-Member -InputObject $result NoteProperty 'Uptime' $reboot_time

Hi it is working.

can you explain “2>&1” part ?


There are different Output-Pipes in PS


1 means the standard output pipe
2 means the error output pipe

I redirect the output from ErrorOutputPipe to the StandardOutputPipe

Use calculated expressions to rename the columns or perform conversions:

$serverList = 'Computer1'
$results = Foreach ($Server in $Serverlist) {

    Get-CimInstance -ClassName Win32_OperatingSystem  -ErrorAction Stop | 
    Select-Object  @{Name='ServerName';Expression={$Server}},
                @{Name='UptimeInDays';Expression={[math]::Round((New-TimeSpan -Start $_.LastBootUpTime -End (Get-Date)).TotalDays)}},


Note that ConvertToDateTime is not required and Powershell does the date conversion and use Get-CimInstance as Get-WmiObject is deprecated.