【问题标题】:How to output multiple hash tables in Powershell如何在 Powershell 中输出多个哈希表
【发布时间】:2012-06-07 16:22:12
【问题描述】:

我有一个键/值对哈希表的哈希表(来自 .ini 文件)。它看起来像这样:

Name                           Value                                                                   
----                           -----                                                                   
global                         {}                                                                      
Variables                      {event_log, software_repo}                                               
Copy Files                     {files.mssql.source, files.utils.source, files.utils.destination, fil...

如何在一个哈希表中输出所有键/值对,而不是这样做?

$ini.global; $ini.variables; $ini."Copy Files"

【问题讨论】:

    标签: powershell


    【解决方案1】:

    鉴于$hh 是哈希表的哈希表,您可以使用循环:

    foreach ($key in $hh.keys)
    {
      Write-Host $hh[$key]
    }
    

    【讨论】:

    • 谢谢,我把它简化为$hh.keys | %{ $hh.$_ }
    【解决方案2】:

    您可以轻松编写一个函数以通过哈希进行递归:

    $hashOfHash = @{
        "outerhash" = @{
            "inner" = "value1"
            "innerHash" = @{
                "innermost" = "value2"
            }
        }
        "outer" = "value3"
    }
    
    function Recurse-Hash($hash){
        $hash.keys | %{
            if($hash[$_] -is [HashTable]){ Recurse-Hash $hash[$_]  }
            else{
                write-host "$_ : $($hash[$_])"
            }
        }
    }
    
    Recurse-Hash $hashOfHash
    

    以上只是write-hosting 键值,但你明白了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-26
      • 1970-01-01
      • 2021-10-15
      • 2021-06-29
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      相关资源
      最近更新 更多