Search code examples
jsonpowershellazure-logic-apps

powershell script -replace a json array (escape $var to be treated as a text without double quote)


I have solve this topic (json template reference to an object variable;) and my problem is now a bit related to previous topic because I need somehow to use same template json used in the generator powershell script below:

function DownloadLogicAppJson([string] $resourceGroup, [string] $logicAppName, [string] $fileName) {

Write-Host "Downloading Logic App: $logicAppName"
Write-Host 'Resource Group:' $resourceGroup
Write-Host 'Output File: ' $fileName

$logicApp = Get-AzLogicApp -ResourceGroupName $resourceGroup -Name $logicAppName
$logicAppText = $logicApp.Definition.ToString()
$logicAppDefinition = $logicApp.Definition

$logicAppText = $logicAppText -replace '"timeZone": ".*?"', '"timeZone": "${scheduleTimezone}"'       #timezone is a String variable;
$logicAppText = $logicAppText -replace '"body": .*?,', '"body": ${requestBody},'  #requestBody is an Object variable;

#$logicAppText = $logicAppText -replace '"hours": [.*]', '"hours": [$scheduleHour]' #a string variable;
$logicAppDefinition = $logicAppText | ConvertFrom-Json

$logicAppText = $logicAppDefinition | ConvertTo-Json -Depth 100
$logicAppText | Out-File -FilePath .\$fileName  

Write-Host "Logic App definition JSON downloaded and saved to: $fileName"

}

DownloadLogicAppJson -resourceGroup $resourceGroup -logicAppName $logicAppName -fileName $outputFileName

My problem is on that variable scheduleHours and requestBody because they cannot be treated as I deployed my initial json when I deployed logic app: Initially (solution agreed):

"recurrence": { "frequency": "Day", "interval": 1, "schedule": { "hours": [ ${scheduleHour} #WITHOUT DOUBLEQUOTES; here is a number var; ], "minutes": [ ${scheduleMinute} #WITHOUT DOUBLEQUOTES; here is a number var; ] }, "timeZone": "${scheduleTimezone}" #here is a string, works; }, "type": "Recurrence"

After I deployed the above code-part, I have in Azure Portal in Logic App code view like:

"recurrence": { "frequency": "Day", "interval": 1, "schedule": { "hours": [ 15 ], "minutes": [ 0 ] }, "timeZone": "W. Europe Standard Time"

Ok, If I run the above powershell script to generate the new JSON template from the portal logic app, it takes as it is with values but I need to be parameterized because I have more than 1 logic app and they have different values because I have there different tfvars used for each environment. I want to be a general template generated parameterized, not with values.

*I know the JSON deployed initially is not a good because it has no "" used for scheduleHours , but it works to be deployed and to reference that values of $scheduleHours . How can I do in powershell this replacement?

I've tried also with:

$logicAppText = $logicAppText -replace '"hours": [.*?]', '"hours": [${scheduleHour}]' -> generating as "hours": [15]

I need to generate it as: "hours": ${scheduleHour}

I've tried another approach:

$logicAppDefinition = $logicApp.Definition $logicAppDefinition.triggers.Recurrence.recurrence.schedule.hours = @($scheduleHour) -> generates "hours": [ null ];

Tried to escape like: $logicAppText = $logicAppText -replace '"hours": [.*?]', '"hours": [`${scheduleHour}]' but not working, it generates with value 15;

I am not sure also about [.*?] is right one;

I tried with: $logicAppDefinition.triggers.Recurrence.recurrence.schedule.hours = @('$scheduleHour') not working as expecting because it generates:"hours": [ "$scheduleHour" ] where I need without double quotes, like $scheduleHour


Solution

  • I tried with: $logicAppDefinition.triggers.Recurrence.recurrence.schedule.hours = @('$scheduleHour') not working as expecting because it generates: "hours": [ "$scheduleHour" ] where I need without double quotes, like $scheduleHour

    I'd go for this approach and then use -replace to remove the "'s around the variable expressions afterwards:

    $logicAppDefinition = $logicApp.Definition
    $logicAppDefinition.triggers.Recurrence.recurrence.schedule.hours = @('$scheduleHour')
    
    # use regex to replace `"$scheduleHour"` with `$scheduleHour`
    $logicAppUpdatedText = ($logicAppDefinition |ConvertTo-Json -Depth 100) -replace '"$(\w+)"','$$$1'
    
    $logicAppUpdatedText |Out-File -FilePath .\$fileName