2016-03-04 66 views
1

我在PowerShell中實現HashMap時遇到了問題。我創建了HasMap從靜止效應初探使用PowerShell中的HashMap

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

我成功驗證使用

$response_connection_hashmap.GetEnumerator()|Sort-Object Name 

的hasmap然而,當搜尋通過鍵的值,我使用下面

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

回答

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) } 
+0

謝謝你的快速幫助! – live2learn