I have a json file in which i would like to change values and save again as a Json: Values that need to be updated:
domain
repo
[ { "name": "[concat(parameters('factoryName'), '/LS_New')]", "type": "Microsoft.DataFactory/factories/linkedServices", "apiVersion": "2018-06-01", "properties": { "description": "Connection", "annotations": [], "type": "AzureDatabricks", "typeProperties": { "domain": "https://url.net", "accessToken": { "type": "AzureKeyVaultSecret", "store": { "referenceName": "LS_vault", "type": "LinkedServiceReference" }, "secretName": "TOKEN" }, "newClusterNodeType": "Standard_DS4_v2", "newClusterNumOfWorker": "2:10", "newClusterSparkEnvVars": { "PYSPARK_PYTHON": "/databricks/python3/bin/python3" }, "newClusterVersion": "7.2.x-scala2.12" } }, "dependsOn": [ "[concat(variables('factoryId'), '/linkedServices/LS_evaKeyVault')]" ] }, { "name": "[concat(parameters('factoryName'), '/PIP_Log')]", "type": "Microsoft.DataFactory/factories/pipelines", "apiVersion": "2018-06-01", "properties": { "description": "Unzip", "activities": [ { "name": "Parse", "description": "This notebook", "type": "DatabricksNotebook", "dependsOn": [], "policy": { "timeout": "7.00:00:00", "retry": 0, "retryIntervalInSeconds": 30, "secureOutput": false, "secureInput": false }, "userProperties": [], "typeProperties": { "notebookPath": "/dataPipelines/main_notebook.py", "baseParameters": { "businessgroup": { "value": "@pipeline().parameters.businessgroup", "type": "Expression" }, "project": { "value": "@pipeline().parameters.project", "type": "Expression" } }, "libraries": [ { "pypi": { "package": "cytoolz" } }, { "pypi": { "package": "log", "repo": "https://b73gxyht" } } ] }, "linkedServiceName": { "referenceName": "LS_o", "type": "LinkedServiceReference" } } ], "parameters": { "businessgroup": { "type": "string", "defaultValue": "test" }, "project": { "type": "string", "defaultValue": "log-analytics" } }, "annotations": [] }, "dependsOn": [ "[concat(variables('factoryId'), '/linkedServices/LS_o')]" ] } ]
I tried using regex but i am only able to update 1 value :
<valuesToReplace>
<valueToReplace>
<regExSearch>(/PIP_Log[wW]*?[pP]roperties[wW]*?[lL]ibraries[wW]*?[pP]ypi[wW]*?"repo":s)"(.*?[^\])"</regExSearch>
<replaceWith>__PATValue__</replaceWith>
</valueToReplace>
<valueToReplace>
<regExSearch>('/LS_New[wW]*?[pP]roperties[wW]*?[tT]ypeProperties[wW]*?"domain":s"(.*?[^\])")</regExSearch>
<replaceWith>__LSDomainName__</replaceWith>
</valueToReplace>
</valuesToReplace>
Here is the powershell code. The loop goes through all the values that are to be replaced. I tried using dynamic variable in select-string and looping, but it doesn't seem to work
foreach($valueToReplace in $configFile.valuesToReplace.valueToReplace)
{
$regEx = $valueToReplace.regExSearch
$replaceValue = '"' + $valueToReplace.replaceWith + '"'
$matches = [regex]::Matches($json, $regEx)
$matchExactValueRegex = $matches.Value | Select-String -Pattern """repoD:s*(.*)" | % {$_.Matches.Groups[1].Value}
$updateReplaceValue = $matches.Value | Select-String -Pattern "repoD:sD__(.*)__""" | % {$_.Matches.Groups[1].Value}
$updateReplaceValue = """$patValue"""
$json1 = [regex]::Replace($json, $matchExactValueRegex , $updateReplaceValue)
$matchExactValueRegex1 = $matches.Value | Select-String -Pattern """domainD:s*(.*)" | % {$_.Matches.Groups[1].Value}
$updateReplaceValue1 = $matches.Value | Select-String -Pattern "domainD:sD__(.*)__""" | % {$_.Matches.Groups[1].Value}
$updateReplaceValue1 = """$domainURL"""
$json = [regex]::Replace($json1, $matchExactValueRegex1 , $updateReplaceValue1)
}
else
{
Write-Warning "Inactive config value"
}
$json | Out-File $armFileWithReplacedValues
Where am i missing??
See Question&Answers more detail:os