我一直在尝试解决这个问题,但几天来一直找不到可行的方法,所以希望您能够提供帮助或指引我正确的方向。
我正在创建一个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。
希望这一切都讲得清楚明白。