Unable to get the Output of Hashtable in Table format

output of below script is showing in hashtable format.

param(
    [string[]]$computername
)
foreach($computer in $computername){
$filebeat_status_before = get-service -name filebeat -computername $computer
get-service -name filebeat -computername $computer |Restart-Service
$filebeat_status_after = get-service -name filebeat -computername $computer

$props = [ordered]@{'computername'=$computer;'status_before'=$filebeat_status_before.Status;'status_after'=$filebeat_status_after.status
            }
$obj = New-Object -TypeName PSobject -Property $props

write-output "$obj"
}

Output is showing as below
@{computername=XYZ; status_before=Running; status_after=Running}

but it should show as below:
computername = XYZ
status_before = Stopped
status_after = Running

Any thoughts how to fix this output format.

Anudeep,
Welcome to the forum. :wave:t4:

I’d use a [PSCustomObject] instead of a hashtable in such a case. It’s even easier to read and maintain.

param(
    [string[]]$computername
)
foreach ($computer in $computername) {
    $filebeat_status_before = Get-Service -Name filebeat -ComputerName $computer
    Get-Service -Name filebeat -ComputerName $computer | Restart-Service
    $filebeat_status_after = Get-Service -Name filebeat -ComputerName $computer

    [PSCustomObject]@{
        ComputerName  = $computer
        Status_Before = $filebeat_status_before.Status
        Status_After  = $filebeat_status_after.Status
    }
}
1 Like