Author Posts

August 21, 2015 at 5:17 pm

I have hashtable which is returned from execution of remote command. Yet when I issue Select-Object command it does not pull those properties out. It does work on some objects and not on others (I execute the same script on range of computers). And I can not figure out what the difference between those.

PS C:\> $b.GetType()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Hashtable System.Object

PS C:\> $b.Keys
PS C:\> $b.Values
PS C:\> $b | Select-Object -Property LogNameForSource, MachineName, SourceExists | ft -AutoSize

LogNameForSource MachineName SourceExists
---------------- ----------- ------------

August 21, 2015 at 5:24 pm

I found what the difference is – one some computers using Invoke-Command returns PSCustomObject and on others it returns HashTable code is below

$temp = {$result= [pscustomobject]@{"MachineName" = $env:ComputerName; "SourceExists" = [bool] $false;"LogNameForSource"=""}
if ([System.Diagnostics.EventLog]::SourceExists('dcom'))
$result.SourceExists = $true
$result.LogNameForSource = [System.Diagnostics.EventLog]::LogNameFromSourceName("dcom", ".")
return $result

$a = Invoke-Command -ScriptBlock $temp -ComputerName Server1
PS C:\> $a.GetType().Name
$a = Invoke-Command -ScriptBlock $temp -ComputerName Server2
PS C:\> $a.GetType().Name

August 22, 2015 at 5:05 am

And a hash table only has Key and Value properties. You can't select just a particular key using Select-Object.

August 22, 2015 at 7:11 am


Check out the official documentation regarding hash tables for examples:

August 24, 2015 at 12:44 am

GS, on these computers you probably does not have latest PS

PS C:\Users> $psversiontable
Name Value
---- -----
PSVersion 2.0
PS C:\Users> [pscustomobject]@{aaa='aaaa';bbb='bbbb'}
Name Value
---- -----
bbb bbbb
aaa aaaa
PS C:\Users> ([pscustomobject]@{aaa='aaaa';bbb='bbbb'}).gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Hashtable System.Object

[pscustomobject] was implemented on v3+