我正在努力用PowerShell将一些JSON代码添加到现有文件中.
下面是必须添加到文件中特定位置的json块.
$manualInterventionJson = @"
{
"deploymentInput": {
"parallelExecution": {
"parallelExecutionType": 0
},
"timeoutInMinutes": 0,
"jobCancelTimeoutInMinutes": 1,
"condition": "succeeded()",
"overrideInputs": {}
},
"rank": 2,
"phaseType": 2,
"name": "Agentless job",
"refName": null,
"workflowTasks": [
{
"environment": {},
"taskId": "bcb64569-d51a-4af0-9c01-ea5d05b3b622",
"version": "8.*",
"name": "Manual Intervention",
"refName": "",
"enabled": true,
"alwaysRun": false,
"continueOnError": false,
"timeoutInMinutes": 0,
"retryCountOnTaskFailure": 0,
"definitionType": "task",
"overrideInputs": {},
"condition": "succeeded()",
"inputs": {
"instructions": "test blablabla",
"emailRecipients": "",
"onTimeout": "reject"
}
}
]
}
"@
Json文件具有复杂的 struct .以下是一个示例版本:
{
"source": 2,
"variables": {},
"environments": [{
"name": "Deploy",
"rank": 1,
"deployPhases": [{
"rank": 1,
"phaseType": 1,
"name": "Agent job",
"refName": null,
"workflowTasks": [{
"environment": {},
"taskId": "##TASK_GROUP_ID##",
"version": "1.*",
"name": "##TASK_GROUP_NAME##",
"condition": "succeededOrFailed()",
"inputs": {}
}]
}],
"postDeploymentGates": {
"id": 0,
"gatesOptions": null
},
"environmentTriggers": []
}]
}
请注意,目前只有1个部署阶段(级别1),必须添加级别2,即顶部的json块.
This may also help in replicating the issue:
这就是最终的结果:
我用.Add()
来测试数组,用+=
或Add-Member
来测试,我甚至try 创建一个新的字典对象$obj = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
,但这也不起作用,因为有些值是不同类型的.
编辑:PS.我不需要保存更新后的文件.只需读取文件$jsonObj = Get-Content "C:\temp\test.json" -Raw | ConvertFrom-Json
,更新变量中的内容,无需保存到文件中.
在这一点上,我可以得到一些指导.谢谢.