PowerShell 7.3 - Unexpected Behavior when Using Invoke-Command with Remote Sessions and Output Formatting
I'm a bit lost with I'm testing a new approach and I'm writing unit tests and I'm stuck on something that should probably be simple..... I'm encountering an issue with using `Invoke-Command` in PowerShell 7.3 for executing scripts on remote machines. I have a script that retrieves system information from multiple servers and formats the output into a custom object. However, when I try to use `Invoke-Command`, the output seems to lose its structure, and I'm getting unexpected results. Here's a simplified version of what I'm trying to do: ```powershell $servers = "Server1", "Server2" $scriptBlock = { [PSCustomObject]@{ Hostname = $env:COMPUTERNAME OSVersion = (Get-CimInstance Win32_OperatingSystem).Version Uptime = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime } } $result = Invoke-Command -ComputerName $servers -ScriptBlock $scriptBlock $result ``` While executing this code, I expect to see a nicely formatted output with the hostname, OS version, and uptime for each server, but instead, the output is flattened and appears like this: ``` Hostname : Server1 OSVersion : 10.0.19042 Uptime : 2023-10-01 12:30:00 Hostname : Server2 OSVersion : 10.0.19042 Uptime : 2023-10-01 12:45:00 ``` It looks like the output is being combined into a single table instead of keeping separate objects for each server. I've tried wrapping the entire command in a `ForEach-Object`, but that didn't seem to help. Is there a way to maintain the structure of the output when using `Invoke-Command`, or am I missing something in the way I'm calling the script block? Any suggestions would be greatly appreciated! Am I missing something obvious? The stack includes Powershell and several other technologies. The project is a desktop app built with Powershell. Any ideas what could be causing this?