`Select Name -ExcludeProperty PSComputerName, RunspaceId` -- is not excluding

The experience below happens with both PS 5.1 and PS 7 on Win-11\Home.

THE GOAL: For “-ExcludeProperty PSComputerName, RunspaceId” to actually work and omit those values from the ouput. Both of the lines below output the same data.

PS> Invoke-Command -ComputerName Pc1, Pc2 -ScriptBlock { CimInstance -ClassName Win32_ComputerSystem | select name -ExcludeProperty PSComputerName, RunspaceId }

name  PSComputerName RunspaceId
----  -------------- ----------
Pc1   123.456.78.333 f517f770-37e4-4bd2-a389-939c2465c05c
Pc2   Pc2            101e2a4d-8817-4cab-ba82-a877c0b6f8f1

PS> Invoke-Command -ComputerName Pc1, Pc2 -ScriptBlock { CimInstance -ClassName Win32_ComputerSystem | select name } 

name  PSComputerName RunspaceId
----  -------------- ----------
Pc1   123.456.78.333 33bf5ef8-4688-4d30-b405-13b82b31d3c6
Pc2   Pc2            54b296a8-d0b6-4825-ad13-1968439083f8

The second concern, with the same code (above), respectively the output for Pc1 has different values for the ‘Name’ and ‘PSComputerName’ properties; usually they are the same; as evidenced with the Pc2 output. To be clear, Pc1 is outputting an ip for the PSComputerName property-value; instead of the computers’ name. Where should I look to resolve this; in the OS or, PowerShell?

Respectfully

Please share the complete code you’re using.

… to resolve what? … that you get an IP instead of the name of the remote computer or the fact that -ExcludeProperty does not seem to work as you expect it - like your subject implies.

@Olaf – Apologies, I’ve updated the original post, you can now see the full code and compare. I incorrectly used the block code, cutting-off the first command-line. I am using the code on the cli.

Both, I was trying to not create separate posts, I’m not sure if both of these issue’s are caused by the code or something else.

Hmmm … ok, the first issue you have comes from the wrong usage of the parameter -ExcludeProperty. You command should look like this:

Invoke-Command -ComputerName Pc1, Pc2 -ScriptBlock { 
    Get-CimInstance -ClassName Win32_ComputerSystem  
} | 
    Select-Object -Property Name -ExcludeProperty PSComputerName, RunspaceId

Actually you don’t need the Invoke-Command at all since the ..-Cim... cmdlets can do remoting by themselfs …

Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName Pc1, Pc2 |
    Select-Object -Property Name

The other issue comes pretty likely from an issue with your DNS resolution. At least I cannot reproduce this behaviour. :man_shrugging:

1 Like

@Olaf – I used Invoke-Command with the thought of adding more code to the ScriptBlock, that would include at least two Get-CimInstance call’s from different classes. I am not yet sure which Classes will be used, which is why I stopped here with the -Exclude concern before adding more code, further complicating the trouble-shooting process.

After applying your suggestion I managed to break it again by adding code to the Invoke-Command -ScriptBlock { ... } I’ll mark this thread as resolved.

Thank you

Since we’re already on it … show your code, show the errors you get and explain what you’re actually trying to do … there might be a better way.

For example:

If you only want to get information about remote systems covered by WMI/CIM you could create CIM-Sessions for the remote computers and run the Get-CimInstance commands against these CIM-Sessions. Quite often that even speeds up the code execution.

@Olaf – Will be pulling data from both the PC running the script and others in the same WorkGroup. The initial post was the beginning of a header or, a pre-script, that would be placed before the main code in a Ps1. With the correction you provided, I’m currently using the following:

PS> Invoke-Command -ComputerName 123.456.xx.xxx, Pc2 -ScriptBlock { CimInstance -ClassName Win32_ComputerSystem | select Name; Get-CimInstance -ClassName Win32_logicaldisk -Filter "DriveType =3" } | Select-Object name, @{Name="FreeSpace (GB)";Expression={[math]::Round($_.FreeSpace / 1GB, 2)}} -ExcludeProperty PSComputerName, RunspaceId

With the exception of the 0.00 output for the respective PC name’s, I can live/work with this, until time allows for further research --and learn how to omit the null field for the PC name’s; perhaps using a conditional. I’ll continue and build on the above.

Olaf, Thank You.

But that’s actually the point I’m trying to make … you may overcomplicate your task …

For example: Just to get the name of the remote computer you don’t need to query Win32_ComputerSystem explicitly. The name of the system is returned by every other WMI-Class as well. So the code you have so far could be boiled down to something like this:

$CimSessionList = New-CimSession -ComputerName 123.456.xx.xxx, Pc2

Get-CimInstance -ClassName Win32_logicaldisk -Filter 'DriveType =3' -CimSession $CimSessionList |
    Select-Object -Property SystemName, DeviceID, 
    @{Name = 'Size (GB)'; Expression = { [math]::Round($_.Size / 1GB, 2) } } ,
    @{Name = 'FreeSpace (GB)'; Expression = { [math]::Round($_.FreeSpace / 1GB, 2) } }

Regardless of that … you should do yourself and others a favor and format your code nicely. To ram it all in one long line just makes it harder to read, to understand and to maintain.
You may read more about style and best practices in the:

@Olaf – I agree, I’m also told I over think things. It’s do to my lack of understanding a certain something. Additional annotation’s below.

Olaf – I’m still struggling, with typo’s and silly syntax error’s like the one you corrected earlier in this thread. I have yet to really research Get-Help *Cim*. The only reason I used it here, is that I remembered your suggestion from another earlier post, about how Get-CimInstance was more efficient/faster than Get-ComputerInfo. This is my first attempt at actually using it beyond a short one-liner.

I do appreciate the version you provided, but I am trying to actually have some code --close to working, before posting a thread. I try real hard not to post queries that appear to ask for free code without showing I put-in some valid effort. I am actually going to buid-out from what I started, as well as, your offering.

Olaf – always did have a problem with that exact point. I feel lucky to have the code resemble the expected output. Once it works, I pretty-much leave well enough alone before I break something, again and again …

Olaf – I hope to get the uninterrupted time to read and absorb its contents.

Thank You; again :wink:

What IDE are you using? I’d recommend using VSCode as it helps avoiding a lot of typos and silly mistakes.

@Olaf – None (yet), lifes’ plate is overflowing, not much excess time to learn VSCode; still using a text editor.

That’s a really bad idea. :index_pointing_up:

There’s not that much to learn. Simply install it, add the proper PowerShell extension and use it. It will improve your coding skills right away.

2 Likes