Reputation: 911
I am facing issues while implementing a HashMap in PowerShell. I have created the HasMap from a ReST reponse using
$response_connection_hashmap = $response_connection|foreach {
@{ $_.name = $_.id }
}
I am successfully verifying the hasmap using
$response_connection_hashmap.GetEnumerator()|Sort-Object Name
However while searching for a value by key, I am using below
$response_connection_hashmap.Item("Key01")
Getting below error
Exception getting "Item": "Cannot convert argument "index", with value:
"Key01", for "get_Item" to type "System.Int32": "Cannot convert value
"Key01" to type "System.Int32". Error: "Input string was not in a correct
format."""
Upvotes: 1
Views: 1731
Reputation: 20256
You are generating an array of hashtables:
$response_connection_hashmap.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
Try this instead:
$response_connection_hashmap = @{}
$response_connection|foreach { $response_connection_hashmap.add($_.name, $_.id) }
Upvotes: 2