Wednesday, September 21, 2011

Powershell PSComputerName is empty or blank when selected after invoke-command

When using PowerShell remoting a NoteProperty object is attached to each result to indicate the computer the result was generate on.  This property is empty or blank when a select object is done further down the pipeline, but inspecting the object shows the data is present.  Here is an example:
invoke-command -computername MyServer -scriptblock {gwmi win32_operatingsystem}
SystemDirectory : C:\WINDOWS\system32
Organization    : IT
BuildNumber     : 3790
RegisteredUser  : User
SerialNumber    : 69713-640-2887872-45140
Version         : 5.2.3790
PSComputerName  : MyServer

invoke-command -computername MyServer -scriptblock {gwmi win32_operatingsystem} | Select PSComputerName, Version

A simple way to work around this problem is to use the format-table cmdlet:
invoke-command -computername MyServer -scriptblock {gwmi win32_operatingsystem} | Format-Table PSComputerName, Version
PSComputerName                                                                                      Version
--------------                                                                                      -------
MyServer                                                                                            5.2.3790

No comments:

Post a Comment