小编A.L*_*say的帖子

将 Powershell 转换为嵌套的 Json

所以我已经尝试解决这个问题几天了,但似乎找不到任何有效的方法,所以我希望你能帮助我或指出正确的方向。

我正在创建一个在 Active Directory 中创建服务帐户的 powershell 脚本,在脚本的最后是用户名和密码,然后存储在 Enterprise Password Vault 中。REST Api 用于在此 Vault 中存储凭据。我在将我的 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"},
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 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 …
Run Code Online (Sandbox Code Playgroud)

powershell json

3
推荐指数
1
解决办法
8104
查看次数

标签 统计

json ×1

powershell ×1