This may be a dumb question so please be kind LOL. I'm trying to wrap my head around something I just ran into.
First
I have a HashTable that I can enumerate using the following:
$ht = [hashtable]@{"Key1"="Value1";"Key2"="Value2"}
$ht.GetEnumerator()
Name Value
---- -----
Key1 Value1
Key2 Value2
If I store this to a variable, it exists only for one call of the variable.
$KeyPairs = $ht.GetEnumerator()
$KeyPairs
Name Value
---- -----
Key1 Value1
Key2 Value2
$KeyPairs
# Nothing is returned as if $KeyPairs lost its value
Can someone help me understand why that is?
Second
Typically with a collection, I sometimes want to target a single item for testing (such as viewing properties of one instance), and I can use Select-Object or via index:
$array = @("Value1","Value2")
$array | select -first 1
Value1
$array[0]
Value1
The hashtable enumerator seems to only support the Select-Option, not the index.
$ht = [hashtable]@{"Key1"="Value1";"Key2"="Value2"}
$ht.GetEnumerator() | Select -first 1
$ht.GetEnumerator() | Select -first 1
Name Value
---- -----
Key1 Value1
($ht.GetEnumerator() | measure).count
2
($ht.GetEnumerator())[0]
Name Value
---- -----
Key1 Value1
Key2 Value2
Can someone explain that as well? Why can't I use the index option here? $ht.GetEnumerator() returns a Dictionary collection, and when using Select -first 1, it returns a single DictionaryEntry, but when referencing the index, it returns both dictionary entries.
$KeyPairs.Reset()and try to print$KeyPairsagain to get the expected behaviour