The code below produce 2 "identical" Hashtables, however on the one that was grouped using a code block I can't get items from the key.
$HashTableWithoutBlock =
Get-WmiObject Win32_Service | Group-Object State -AsHashTable
$HashTableWithBlock =
Get-WmiObject Win32_Service | Group-Object {$_.State} -AsHashTable
Write-Host "Search result for HashTable without using code block : " -NoNewline
if($HashTableWithoutBlock["Stopped"] -eq $null)
{
Write-Host "Failed"
}
else
{
Write-Host "Success"
}
Write-Host "Search result for HashTable with code block : " -NoNewline
if($HashTableWithBlock["Stopped"] -eq $null)
{
Write-Host "Failed"
}
else
{
Write-Host "Success"
}
Output :
Search result for HashTable without using code block : Success
Search result for HashTable with code block : Failed
What is the difference between the two Hashtables ?
How to get Items on second one that was grouped by code block ?
EDIT : More than a workaround, I'd like to know if it is possible to retrieve the Item I want with a table lookup, and if yes, how ?
The other posters are correct that the problem is with the key being stored as a
PSObject
but there is a built-in solution for this: use the-AsString
switch along with-AsHashTable
. This will force the key to be stored as a string. You can take a look at the code hereI've opened an issue on GitHub for this bug.
The difference between two
Hashtable
s is that$HashTableWithBlock
have its key wrapped inPSObject
. Problem is that PowerShell normally unwrapPSObject
before pass it to the method call, so even if you have right key, you still can not just pass it to indexer. To workaround this you can create helper C# method what would call indexer with right object. Another way is to use reflection:Here's a workaround I found, which is really not great:
I mean I guess once you're doing
ForEach-Object
you almost might as well just build a hashtable yourself?Do note that, interestingly, this will not work if you group on a ScriptProperty instead. I haven't figured out why.