Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用powershell替换Json中的值_Powershell - Fatal编程技术网

使用powershell替换Json中的值

使用powershell替换Json中的值,powershell,Powershell,我有一个json文件,我想在其中更改值并再次保存为json: 需要更新的值: 领域 回购 [ { "name": "[concat(parameters('factoryName'), '/LS_New')]", "type": "Microsoft.DataFactory/factories/linkedServices", "apiVersion"

我有一个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>
    

    我在哪里丢失了???

    你不应该直接偷看和戳入序列化文件(例如Json文件)。改为使用cmdlet反序列化文件,对对象进行更改并使用cmdlet再次序列化:


    JSON是一种数据结构。您不使用正则表达式,因为
    convertfromjson
    创建了一个漂亮的PS对象。
    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
    
    $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