powershell - 使用 powershell 替换 Json 中的值
问题描述
我有一个 json 文件,我想在其中更改值并再次保存为 Json:需要更新的值:
领域
回购
[ { "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')]" ] } ]
我尝试使用正则表达式,但我只能更新 1 个值:
<valuesToReplace>
<valueToReplace>
<regExSearch>(\/PIP_Log[\w\W]*?[pP]roperties[\w\W]*?[lL]ibraries[\w\W]*?[pP]ypi[\w\W]*?"repo":\s)"(.*?[^\\])"</regExSearch>
<replaceWith>__PATValue__</replaceWith>
</valueToReplace>
<valueToReplace>
<regExSearch>('\/LS_New[\w\W]*?[pP]roperties[\w\W]*?[tT]ypeProperties[\w\W]*?"domain":\s"(.*?[^\\])")</regExSearch>
<replaceWith>__LSDomainName__</replaceWith>
</valueToReplace>
</valuesToReplace>
这是powershell代码。循环遍历所有要替换的值。我尝试在选择字符串和循环中使用动态变量,但它似乎不起作用
foreach($valueToReplace in $configFile.valuesToReplace.valueToReplace)
{
$regEx = $valueToReplace.regExSearch
$replaceValue = '"' + $valueToReplace.replaceWith + '"'
$matches = [regex]::Matches($json, $regEx)
$matchExactValueRegex = $matches.Value | Select-String -Pattern """repo\D:\s*(.*)" | % {$_.Matches.Groups[1].Value}
$updateReplaceValue = $matches.Value | Select-String -Pattern "repo\D:\s\D__(.*)__""" | % {$_.Matches.Groups[1].Value}
$updateReplaceValue = """$patValue"""
$json1 = [regex]::Replace($json, $matchExactValueRegex , $updateReplaceValue)
$matchExactValueRegex1 = $matches.Value | Select-String -Pattern """domain\D:\s*(.*)" | % {$_.Matches.Groups[1].Value}
$updateReplaceValue1 = $matches.Value | Select-String -Pattern "domain\D:\s\D__(.*)__""" | % {$_.Matches.Groups[1].Value}
$updateReplaceValue1 = """$domainURL"""
$json = [regex]::Replace($json1, $matchExactValueRegex1 , $updateReplaceValue1)
}
else
{
Write-Warning "Inactive config value"
}
$json | Out-File $armFileWithReplacedValues
我在哪里失踪??
解决方案
您不应该直接在序列化文件(例如 Json 文件)中偷看和戳。而是使用ConvertFrom-Json
cmdlet 反序列化文件,对对象进行更改并使用ConvertTo-Json
cmdlet 再次对其进行序列化:
$Data = ConvertFrom-Json $Json
$Data[0].properties.typeproperties.domain = '_LSDomainName__'
$Data[1].properties.activities.typeproperties.libraries[1].pypi.repo = '__PATValue__'
$Data | ConvertTo-Json -Depth 9 | Out-File $armFileWithReplacedValues
推荐阅读
- php - 如何在 Symfony 中将字符串转换为 HttpFoundation\Request?
- opencv - 如何将从opencv videocapture中提取的帧写入kinesis视频流
- i2c - 我希望在 stm32f407vg 中的 i2c 通信期间忽略确认位
- sql - 存在where子句时postgresql不返回行
- python - 如何使用漂亮的汤4解开父母
- php - 根据产品标签获取类别
- codenameone - 如何使用 Motion 类?
- python - Appium 和 python:查找元素子项
- flutter - 调试失败,退出代码 1
- javascript - 我想从另一个处理函数调用 var