【发布时间】:2022-11-17 20:03:55
【问题描述】:
在我们的 Azure CICD 管道中,我们有一个尝试部署策略的元素。我们在 repo 中有每个策略的 JSON 文件,我们将所有这些 json 文件放在一个文件中作为 CI 的一部分,稍后通过 CD 部署。 PowerShell 不是我写的,而是几年前在现场的一位 Microsoft 顾问写的。
问题是当所有的 JSON 放在一起时,我们会得到一个非法的语法,例如
将代码更改为可以工作并部署,但这意味着我们必须手动检查所有文件,将 [ 替换为 [[:
总之,PowerShell 将所有这些结合在一起,进行一些操作并输出到 artifacts 文件夹中的文件。
这只是 json 的一个小 sn-p,但突出显示了该区域,并且在整个 json 中有很多这样的区域需要替换:
{
"functions": [
],
"variables": {
"location": "UK South"
},
"resources": [{
"properties": {
"displayName": "Allowed Locations for Resources",
"policyType": "Custom",
"mode": "Indexed",
"description": "description.",
"metadata": {
"version": "1.0.0",
"category": "General"
},
"parameters": {
"listOfAllowedLocations": {
"type": "Array",
"metadata": {
"description": "The list of locations that can be specified when deploying resources.",
"strongType": "location",
"displayName": "Allowed locations"
},
"allowedValues": [
"uksouth",
"ukwest"
],
"defaultValue": [
"uksouth",
"ukwest"
]
}
},
"policyRule": {
"if": {
"allOf": [{
"field": "location",
"notIn": "[parameters('listOfAllowedLocations')]"
},
{
"field": "location",
"notEquals": "global"
},
{
"field": "type",
"notEquals": "Microsoft.Resources/subscriptions/resourceGroups"
},
{
"field": "type",
"notEquals": "Microsoft.Resources/b2cDirectories"
}
]
},
"then": {
"effect": "audit"
}
}
},
"name": "Policy1",
"apiVersion": "2019-01-01",
"type": "Microsoft.Authorization/policyDefinitions",
"location": "[variables('location')]"
}]
}
我的 PowerShell 充其量只是入门级的,所以我正在努力让替代品正常工作。
我可以获得有问题的区域并将其替换为Write-Host,但我不知道如何在不把事情弄得一团糟的情况下写回原始对象:
if ($content.properties.policyRule.if.allOf -ne $null){
foreach ($param in $content.properties.policyRule.if.allOf){
Write-Host "were here..................."
#$param = ($param | ConvertTo-Json -Depth 100 | % { [System.Text.RegularExpressions.Regex]::Unescape($_) })
if ($param.notIn -ne $null){
$param.notIn.replace('[', '[[')
Write-Host $param.notIn
}
}
任何建议将不胜感激。
【问题讨论】:
标签: powershell