For the commands that return PSComputerName, like Get-Service
, everything works as expected. But when I execute a command that returns a non-object value, such as Test-Path
returning a Boolean, I can't figure out how to see the resulting value and the computer name of the machine that executed it. If I do:
$Output = Invoke-Command -ComputerName PC1, PC2, PC3 -ScriptBlock { Test-Path 'c:\temp' }
Then $Output
holds:
True
False
True
If I do $Output | Select-Object *
, I get:
@{PSComputerName=PC1; RunspaceId=32cca2f2-57d1-49c3-9b45-ab32483f0d3a; PSShowComputerName=True}
@{PSComputerName=PC2; RunspaceId=3495fa73-3ca3-44e8-97b6-439d4db27b33; PSShowComputerName=True}
@{PSComputerName=PC3; RunspaceId=af960f6d-9d9f-4437-82b0-be29ea3bed23; PSShowComputerName=True}
I have tried various things, but I can't seem to figure out how to output both the return value and the extra object information that contains the PSComputerName so I know which value goes with which machine.
I am trying to run commands on the command line, so I don't really want to use something that requires an entire script and/or running a script on the remote machine...
There are some special characteristics of objects returned from remote sessions. Even though $Output
looks like a plain Boolean there are additional properties attached. I'm not quite sure how it relays the Boolean value but you can for the most part treat it the same. Instead of using *
name the properties:
$Output | Select PSComputerName,@{N = 'Value'; E = { $_ }}
Admittedly this is very convenient.