我正在尝试将此命令作为部署后操作行内脚本在azure vsts发布管道中执行:
powershell -command "&{
$json = Get-Content '.\appsettings.json' -raw | ConvertFrom-Json;
$data = '{"allowedUrls" : [{ "host" : "someUrl","protocol" : "https"},{ "host":"localhost:44394","protocol" : "https"}]}' | ConvertFrom-Json;
$json.allowedUrls = $data.allowedUrls;
$json | ConvertTo-Json -Depth 32 | Set-Content '.\appsettings.json';
}" 当我在powershell中运行它时,它在本地工作,但在azure上我得到:
Standard error from script:
2020-06-09T08:27:08.5939074Z ConvertFrom-Json : Invalid JSON primitive: someUrl.
2020-06-09T08:27:08.5939545Z At line:1 char:286
2020-06-09T08:27:08.5940773Z + ... host:localhost:44394,protocol : https}]}' | ConvertFrom-Json; Write- ...
2020-06-09T08:27:08.5941450Z + ~~~~~~~~~~~~~~~~
2020-06-09T08:27:08.5942156Z + CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentEx
2020-06-09T08:27:08.5942641Z ception
2020-06-09T08:27:08.5943137Z + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Co
2020-06-09T08:27:08.5943649Z mmands.ConvertFromJsonCommand可能出了什么问题?
当像$data = '{"allowedUrls" : [{"host" :"someUrl","protocol" :"https"},{"host":"localhost:44394","protocol" :"https"}]}' | ConvertFrom-Json;这样的转义字符串出现时
我得到了
Standard error from script:
2020-06-09T09:13:30.3130099Z ConvertFrom-Json : Invalid object passed in, ':' or '}' expected. (4): [{
2020-06-09T09:13:30.3131022Z `host` : `someUrl`,`protocol` : `https`},{ `host`:`localhost:44394`,`protocol`
2020-06-09T09:13:30.3131695Z : `https`}]
2020-06-09T09:13:30.3132284Z At line:1 char:175
2020-06-09T09:13:30.3133099Z + ... :`localhost:44394`,`protocol` : `https`}]' | ConvertFrom-Json; $json. ...发布于 2020-06-09 18:11:26
这就是解决方案:
powershell -command "&{
$json = Get-Content '.\appsettings.json' -raw | ConvertFrom-Json;
$data = '$(additionalAllowedUris)' | ConvertFrom-Json;
$json.additionalAllowedUris= $data.SyncRoot;
$json | ConvertTo-Json -Depth 32 | Set-Content '.\appsettings.json';
}"和管道中的变量:
[{ \"host\" : \"someUrl\",\"protocol\" : \"https\"},{ \"host\":\"localhost:44394\",\"protocol\" : \"https\"},{ \"host\":\"someUrl2\",\"protocol\" : \"https\"}]https://stackoverflow.com/questions/62278381
复制相似问题