Search code examples
powershellazure-devopsazure-pipelinesazure-powershellpolicy

Not able to replace variable values in Azure PowerShell with script arguments to update APIM policies


I have defined my code like this:

param ($apim_service_name, $apprid, $rg_name, $tid, $url)

<set-url>@($"$url/{(string)context.Variables["insured-id"]}")</set-url>
$apimContext = New-AzApiManagementContext -ResourceGroupName $rg_name -ServiceName $apim_service_name
Set-AzApiManagementPolicy -Context $apimContext -Format "application/vnd.ms-azure-apim.policy.raw+xml" -ApiId "echo-api" -OperationId "retrieve-resource" -Policy $PolicyStringRead

In the pipeline variable I have added variables and used in my pipeline templates My PowerShell script looks like this:

steps:
  - task: AzurePowerShell@5
    displayName: 'Azure PowerShell script: FilePath'
      inputs:
         azureSubscription: 'sub-test'
         ScriptPath: 'test-policy.ps1'
         ScriptArguments: '"$(apim_service_name)" "$(apprid)" "$(rg_name)" "$(tid)" -url $(url)'
         azurePowerShellVersion: LatestVersion

Defined variables in variables section of the pipeline

Everything is replacing except the "url"

Is it the two "$" symbols that is affecting to replace the string?

Can anyone help me on how to update this, because it requires first "$" symbol in order to read the value


Solution

  • Thanks @jdweng for suggesting the right solution to the user that resolved the issue.

    PS will not substitue variable inside a single quote (only double quote). Use two double quotes to escape single quote : ScriptArguments: "$apim_service_name $apprid $rg_name $tid -url $url"

    We can enclose a string in both Single Quotation or the double Quotations but to replace the value in the variable that starts with the dollar sign ($) before sending for the command processing, you have to use only double quotes as given in this MS Doc of PowerShell Quoting Rules.