How to persist replaced value in PowerShell

Bill LaLonde 65 Reputation points
2023-08-14T15:08:51.5366667+00:00

I have a JSON file (sample below) that I need to replace a value in. I will have multiple names to sort through and only need to replace a few values.

{
  "name": "[concat(parameters('workspaceName'), '/myLinkedService1')]",
  "type": "Microsoft.Synapse/workspaces/linkedServices",
  "apiVersion": "2019-06-01-preview",
  "properties": {
    "annotations": [],
    "type": "SqlServer",
    "typeProperties": {
      "connectionString": "@{type=AzureKeyVaultSecret; store=; secretName=[parameters('superSecretParams')]}"
    },
    "connectVia": {
      "referenceName": "myIR1",
      "type": "IntegrationRuntimeReference"
    }
  },
  "dependsOn": [
    "[concat(variables('workspaceId'), '/integrationRuntimes/myIR1')]",
    "[concat(variables('workspaceId'), '/linkedServices/BI_Synapse_KeyVault')]"
  ]
}

I tried several scripts to try and replace the referenceName but it seems to only replace the value upon execution of that line but doesn't hold the replaced value in memory so that I can Set-Content in the file.

$jsonFilePath = "C:/Users/me/OneDrive/Desktop/test_file.json"
$jsonData = Get-Content $jsonFilePath -Raw | ConvertFrom-Json

if ($jsonData.name = "[concat(parameters('workspaceName'), '/myLinkedService1')]") {
    $jsonData.properties.connectVia.referenceName.Replace("myIR1", "myIR2") # (value is changed properly)
    Write-Output $jsonData # (value is already changed back here)
    Set-Content ($jsonData | ConvertTo-Json -Depth 4) -Path $jsonFilePath # (doesn't work)
}

Set-Content ($jsonData | ConvertTo-Json -Depth 4) -Path $jsonFilePath # (after running and viewing file, value is still original value)
# This also doesn't work, but I think I'll need to use this ForEach-Object to iterate over all the names.
$jsonFilePath = "C:/Users/me/OneDrive/Desktop/test_file.json"
$jsonData = Get-Content $jsonFilePath -Raw | Out-String | ConvertFrom-Json
ForEach-Object ($name in $jsonData.PSObject.Properties) {
    if ($jsonData.name.Contains("[concat(parameters('workspaceName'), '/myLinkedService1')]")) {
        $jsonData.properties.connectVia.referenceName.Replace("myIR1", "myIR2")
        Write-Output $jsonData.properties.connectVia.referenceName
    }
}
$jsonData | ConvertTo-Json -Depth 32 | Set-Content $jsonFilePath

Any ideas? Maybe assign the conditional to a variable and Set-Content on the variable?

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2023-08-14T21:33:14.4833333+00:00

    Let's see if I can post only a single copy of an answer without angering the forum gods this time!

    You just need to treat "Resources" as the container object that it is:

    $jsonFilePath = "C:/Junk/test_file.json"		# Change this!!
    $jsonData = Get-Content $jsonFilePath -Raw | ConvertFrom-Json
    
    $jsondata.Resources |
        ForEach-Object{
            if ($_.name -EQ "[concat(parameters('workspaceName'), '/myLinkedService1')]") {
                $_.properties.connectVia.referenceName = 
                    $_.properties.connectVia.referenceName.Replace("myIR1", "myIR2")
            }
        }
    Set-Content ($jsonData | ConvertTo-Json -Depth 4) -Path $jsonFilePath
    

    EDIT: replaced "=" with "-EQ". Replaced "$jsonOutFilePath" with the original "$jsonFilePath

    1 person found this answer helpful.

8 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-08-14T19:21:38.9766667+00:00

    Pretty sure this is what you meant to do:

    $jsonFilePath = "C:/Junk/test_file.json"	# change back to your directory!!!
    $jsonData = Get-Content $jsonFilePath -Raw | ConvertFrom-Json
    
    if ($jsonData.name = "[concat(parameters('workspaceName'), '/myLinkedService1')]") {
        $jsonData.properties.connectVia.referenceName = 
            $jsonData.properties.connectVia.referenceName.Replace("myIR1", "myIR2")
        Set-Content ($jsonData | ConvertTo-Json -Depth 4) -Path $jsonFilePath
    }
    

  2. Rich Matheisen 47,901 Reputation points
    2023-08-14T19:21:41.44+00:00

    Ignore this.

    0 comments No comments

  3. Rich Matheisen 47,901 Reputation points
    2023-08-14T19:21:42.7+00:00

    Ignore this too!

    0 comments No comments

  4. Rich Matheisen 47,901 Reputation points
    2023-08-14T19:21:43.55+00:00

    I think something went horribly wrong! Ignore this on (again!)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.