1

I'm doing a Compare-Object where my ref object is a list of strings and the diff object is a hash table

Compare-Object -ReferenceObject $localmd5 -DifferenceObject $remotefilehash | Where-Object { ($_.SideIndicator -eq '=>')} 

The side-indicator gets me all the ones that I'm interested in but what I want is the value from the hash-table returned - how can I do this?

UPDATE 1:

The ask - I need to compare the localmd5 to the hashtable KEYS and return the VALUES

4
  • So, are you saying that you want all the "actual" unique values returned? Commented Mar 18, 2021 at 15:39
  • yes - something like | select-object $remotefilehash.values but only the ones that get passed from the where step Commented Mar 18, 2021 at 15:41
  • Do you want to compare the values stored in HashTable with the strings stored in the other list (Ref object) ? Commented Mar 18, 2021 at 15:48
  • yes thats what the Compare-Object pipe is doing Commented Mar 18, 2021 at 15:49

1 Answer 1

1

Why don't you just convert your HashTable values to a String Array and then compare them both?

  1. Convert your values stored in HashTable to a String array:

    $hashTableValues = $remotefilehash.Values.ForEach('ToString')

  2. Then, simply compare both the String arrays ($localmd5 and $hashTableValues) and get your unique values using Select-Object cmdlet:

    Compare-Object -ReferenceObject $localmd5 -DifferenceObject $hashTableValues | Where-Object { ($_.SideIndicator -eq '=>')} | Select-Object InputObject

As per your updated question:

You can fetch the keys you're interested in by doing something like this:

$requiredKeys = Compare-Object -ReferenceObject $localmd5 -DifferenceObject ($remotefilehash.Keys.GetEnumerator() -join ',').Split(' ') | Where-Object { ($_.SideIndicator -eq '=>')} | Select-Object InputObject

Then, you can convert these keys to an array of strings:

$keysAsString = $requiredKeys | Foreach {"$($_.InputObject)"}

Now, since you've got the list of keys for which you want the values, you can use these keys as indexes and fetch the required values from HashTable:

foreach($key in $keysAsString)
{
    foreach($hKey in $remotefilehash.Keys)
    {
        if($hKey.ToString() -eq $key)
        {
            Write-Output $hash[$hKey] #use the value accordingly
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

sorry b/c I need to compare the localmd5 to the hashtable KEYS and return the VALUES
this doesn't seeem to get me the values only the keys...
I mentioned earlier that you can use the keys as indexes to fetch the values from HashTable, but since you were still confused, I added the code to do that as well (made some minor changes to existing code as well, so please take note of that before trying out the solution).
@Tony - Please mark the answer as Accepted in case it helped you fix your problem. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.