1

在 PowerShell 中实现 HashMap 时遇到问题。我已经使用 ReST 响应创建了 HasMap

$response_connection_hashmap = $response_connection|foreach {
@{  $_.name = $_.id }
}

我正在使用成功验证 hasmap

$response_connection_hashmap.GetEnumerator()|Sort-Object Name

但是,在按键搜索值时,我在下面使用

$response_connection_hashmap.Item("Key01")

低于错误

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."""
4

1 回答 1

2

您正在生成一个哈希表数组:

$response_connection_hashmap.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

试试这个:

$response_connection_hashmap = @{}
$response_connection|foreach { $response_connection_hashmap.add($_.name, $_.id) }
于 2016-03-04T12:17:10.530 回答