将Powershell转换为嵌套的Json

4

我一直在尝试解决这个问题,但几天来一直找不到可行的方法,所以希望您能够提供帮助或指引我正确的方向。

我正在创建一个powershell脚本,在Active Directory中创建服务帐户,脚本的最后阶段会将用户名和密码存储在企业密码保险库中。REST Api用于在此保险库中存储凭据。我有一个小问题:将我的powershell转换为嵌套的Json格式,如果没有嵌套,则可以正常运行......

我需要得到的JSON输出如下:

{
"account":{
    "safe":"Test_safe",
    "platformID":"WindowsDomainAccount",
    "address":"ad.local",
    "password":"password",
    "username":"test",
    "disableAutoMgmt":"true",
    "disableAutoMgmtReason":"test",
    "properties":
    [
    {"Key":"Title", "Value":"Test Account"},
    {"Key":"Description", "Value":"REQ0000001"},
    ]
  }
}

这是我的 Powershell,排除“属性”嵌套,它可以正常运行。

   $hash = [ordered]@{
        account = [ordered]@{
            safe="ServiceAccounts"; 
            platformID="WindowsDomainAccount";
            address="ad.local";
            password="Password1";
            username="svc-test";
            disableAutoMgmt="true";
            disableAutoMgmtReason="testing";
        }
   }

$json = $hash | ConvertTo-Json

$invoke = Invoke-Webrequest -Uri $url -Method "POST" -Body $json -ContentType "application/json" -Header @{"Authorization" = $header} -ErrorAction Stop -ErrorVariable WebError

问题出现在我尝试添加到我的PowerShell时。
"properties":
    [
    {"Key":"Title", "Value":"Test Account"},
    {"Key":"Description", "Value":"REQ0000001"},
    ]

我认为我的第一个问题是“Key”这个词也是PowerShell的关键字。

我尝试了很多不同的组合,并尝试了许多来自其他文章的方法,但我发现的大部分内容都是通过PowerShell解析嵌套的Jason。

希望这一切都讲得清楚明白。

1个回答

7
下面的代码对我有效(PS v4),似乎以你想要的json格式返回结果。
$hash = [ordered]@{
    account = [ordered]@{
        safe="ServiceAccounts"; 
        platformID="WindowsDomainAccount";
        address="ad.local";
        password="Password1";
        username="svc-test";
        disableAutoMgmt="true";
        disableAutoMgmtReason="testing";

        properties = @(
            @{"Key"="Title"; "Value"="Test Account"},
            @{"Key"="Description"; "Value"="REQ0000001"}
        )

    }
}

$json = $hash | ConvertTo-Json -Depth 99
$json

the output of the above...

{
    "account":  {
                    "safe":  "ServiceAccounts",
                    "platformID":  "WindowsDomainAccount",
                    "address":  "ad.local",
                    "password":  "Password1",
                    "username":  "svc-test",
                    "disableAutoMgmt":  "true",
                    "disableAutoMgmtReason":  "testing",
                    "properties":  [
                                       {
                                           "Key":  "Title",
                                           "Value":  "Test Account"
                                       },
                                       {
                                           "Key":  "Description",
                                           "Value":  "REQ0000001"
                                       }
                                   ]
                }
}

谢谢你,我想我被括号的类型弄混了。这是一个简单而愚蠢的错误,但是非常感谢 :) - A.Lindsay
2
“-Depth 99” 是我正在寻找的 :) - Emer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接