1

I'm running an exe from invoke-command on multiple servers and it works. I get a string array with all the output I expect.

$result = invoke-command $servers { & "\path\toexe" arg1 }

This gives me the correct results

str3
str4
str2

...and the PSCOMPUTER name is in there too, but I can't show them together so I can't match the result to the PSComputername all at once.

$Result|GM just shows me PScomputername and RunspaceID as properties. It's like the $result array has no label for the string.

$result | Select * doesn't show me the string

$Result.gettype() shows it is [object] System.Array

I can do $result[0] will show me str3
And then $result[0].PSComputername will show Server3

How do I get that whole array on the screen or dumped to csv together? I'd like to see

PSCOMPUTERNAME   ??
Server3        str3
Server2        str2
0

2 Answers 2

2

You can use Select-Object to create new objects consisting of the attached PSComputerName property along with a new named property containing the intrinsic value of the original string:

$result |Select-Object PSComputerName,@{Name='Value';Expression={"$_"}}

To learn more about using Select-Object with property expressions, see the about_Calculated_Properties help topic

Sign up to request clarification or add additional context in comments.

1 Comment

This is nice and easy - I just thought of 10 more uses for this syntax.
1

I usually package the result in an object. This could be a script too. Unless the object type has a local view file, there's a runspaceid displayed too.

invoke-command $servers { [pscustomobject]@{result = \to` path\toexe arg1} }

result PSComputerName RunspaceId
-----  -------------- ----------
str3   Server3        5f8b7b5e-840d-4790-896a-d68aa23f1c01
str2   Server2        3b37b3d7-f1d0-433a-a387-65c4c3a59a5d

<# script.ps1 #>
[pscustomobject]@{result = \path\toexe arg1}
invoke-command $servers script.ps1

# same result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.