用 PowerShell 替换 ARM 模板中的部分 defaultValue 字符串

Replace part of defaultValue string in ARM Template with PowerShell

我需要替换 ARM 模板文件中“默认值”属性 的特定部分。

我有以下 template.json 例如:

"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "workflows_something_company_upsert_name": {
        "defaultValue": "something-company-upsert",
        "type": "String"
    },
    "workflows_something_app_logging_externalid": {
        "defaultValue": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/RessourceGroupXY/providers/Microsoft.Logic/workflows/something-app-logging",
        "type": "String"
    },
    "connections_commondataservice_1_externalid": {
        "defaultValue": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/RessourceGroupXY/providers/Microsoft.Web/connections/commondataservice-1",
        "type": "String"
    },
    "connections_sql_1_externalid": {
        "defaultValue": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/RessourceGroupXY/providers/Microsoft.Web/connections/sql-1",
        "type": "String"
    }
},
"variables": {},
"resources": [
    {
        "type": "Microsoft.Logic/workflows",
...
...
...
...

我需要实现的是改变“defaultValue”的值:

"defaultValue": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/RessourceGroupXY/providers/Microsoft.Logic/workflows/something-app-logging",

对此:

"defaultValue": "[concat(resourceGroup().id, '/providers/Microsoft.Logic/workflows/something-app-logging')]",

很遗憾,我不是 PowerShell 破解者,所以我希望有人能给我建议,我们将不胜感激。

我自己解决了:

$jsonData = (Get-Content $file.FullName  -Raw) -replace 0x10, (0x13,0x10)
$jsonData = $jsonData -replace '\s',''
$jsonData = [string]::join("",($jsonData.Split("`n")))

$jsonObject = $jsonData | ConvertFrom-Json

foreach($element in $jsonObject.parameters.PSObject.Properties) {

    $found = $element.value.defaultValue -match '(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}'
    
    if(!$found) {
        continue
    }
    
    $workingString = $element.value.defaultValue
    $startIndex = $workingString.IndexOf('/providers')
    $endPath = $workingString.Substring($start, $workingString.Length - $length)
    $element.Value.defaultValue = '[concat(resourceGroup().id, ' + $endPath + ')]'
}