如何将代码中的 C# 字典转换为 Powershell 对象?

How to Convert a C# Dictionary in the code to a Powershell object?

谁能帮帮我?我有构建字典的 C# 代码,我需要将该字典转换为 PowerShell 对象。然后该对象将被传递给 PowerShell 脚本。

我的词典

Dictionary<string, Dictionary<string, string>>

当前版本 (v3/v4) 应该可以直接使用词典,但我不记得 v2 对泛型的处理情况如何。无论如何,这里有一些伪代码(不保证编译)转换为哈希表的哈希表:

var hashtable = new Hashtable();
foreach (var kvPair in dict) {
    var hashtableInner = new Hashtable();
    foreach (var kvPairInner in kvPair.Value) {
        hashtableInner.Add(kvPairInner.Key, kvPairInner.Value);
    }

    hashtable.Add(kvPair.Key, hashtableInner);
}

# Pass hashtable to PowerShell as even v2 understands .NET hashtables