【问题标题】:HashMap in PowerShellPowerShell 中的 HashMap
【发布时间】:2016-03-04 12:05:37
【问题描述】:

我在 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."""

【问题讨论】:

    标签: rest powershell powershell-5.0


    【解决方案1】:

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

    $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) }
    

    【讨论】:

    • 感谢您的快速帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 2019-04-27
    • 2021-06-17
    • 1970-01-01
    • 2011-03-16
    相关资源
    最近更新 更多