Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group-Object diffencies with or without code block [duplicate]

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 ?

like image 477
Luke Marlin Avatar asked Mar 17 '23 06:03

Luke Marlin


2 Answers

The difference between two Hashtables is that $HashTableWithBlock have its key wrapped in PSObject. Problem is that PowerShell normally unwrap PSObject 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:

Add-Type -TypeDefinition @'
    public static class Helper {
        public static object IndexHashtableByPSObject(System.Collections.IDictionary table,object[] key) {
            return table[key[0]];
        }
    }
'@
$HashTableWithBlock = Get-WmiObject Win32_Service | Group-Object {$_.State} -AsHashTable
$Key=$HashTableWithBlock.Keys-eq'Stopped'
#Helper method
[Helper]::IndexHashtableByPSObject($HashTableWithBlock,$Key)
#Reflection
[Collections.IDictionary].InvokeMember('','GetProperty',$null,$HashTableWithBlock,$Key)
like image 188
user4003407 Avatar answered Apr 02 '23 16:04

user4003407


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 here

I've opened an issue on GitHub for this bug.

like image 45
Bruce Payette Avatar answered Apr 02 '23 17:04

Bruce Payette