转换为 PowerShell 字典

Converting to PowerShell Dictionary

我试过使用下面的脚本

$myDictionary = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"
$xml = [xml] (Get-Content 'YOUR_PATH_HERE')     
$xml.SelectNodes("descendant::node()") | ? Value | % $myDictionary.Add($_.ParentNode.ToString(), $_.Value)  }

它对我有用,但它没有解决完整的任务,因为我的 xml 文件具有不同的环境特定信息。 有人可以针对 PowerShell 2.0 版中的上述要求提出可能的解决方案吗?

提前致谢。

您可以尝试对 SelectNodes() 参数使用以下 XPath 表达式('UAT' 环境的示例):

descendant::environment[@id='UAT']/descendant::text()[normalize-space()]

XPath 应return 非空文本节点 在合适的环境中(参见 XPath demo)。