Hey everyone! I'm currently dealing with a problem while setting environment variables in an Azure Container Instance using Azure CLI with PowerShell in an Azure DevOps release task. I've tried the command below to create the container, and it works for one variable that has special characters, but I run into issues with another.
Here's the command I'm using:
az container create `
...
--resource-group "$(RESOURCE_GROUP)" `
--environment-variables `
"BLOBSTORAGE_EMAIL_CONTAINER=`"$(BLOBSTORAGE_EMAIL_CONTAINER)`"" `
"APP_DB_PASSWORD=`"$(APP_DB_PASSWORD)`""
The variable `BLOBSTORAGE_EMAIL_CONTAINER` works perfectly fine, even though it includes special characters like:
wadwad&asda=asd-as:a%
But then there's `APP_DB_PASSWORD`, which contains:
wada"wada^
Using the same format for this one gives me a parsing error. When I try using single quotes like:
'$(APP_DB_PASSWORD)'
It doesn't throw an error, but then I lose the special characters (`"` and `^`) when it gets passed to the container.
I've also tried various methods like using `$env:APP_DB_PASSWORD`, JSON passing, and different quoting approaches, but none have been reliable. Has anyone faced a similar issue or found a secure way to handle environment variables with special characters in Azure CLI for a PowerShell release task? Thanks!
5 Answers
Managing strings like this can really be a hassle! If you need to pass a single value, you might avoid backticks for clarity. Try constructing your command smoothly using a format string where you define your variables clearly:
$container = @'
...
--resource-group "{0}"
--environment-variables
"BLOBSTORAGE_EMAIL_CONTAINER="{1}"
"APP_DB_PASSWORD="{2}"
'@ -f $RESOURCE_GROUP, $BLOBSTORAGE_EMAIL_CONTAINER, $APP_DB_PASSWORD
az container create $container
Have you tried using the `[regex]::Escape()` method in PowerShell? It could help escape the special characters properly.
Here's how you might do it:
```
# Escapes chars for use in a regex
$escapedPattern = [regex]::Escape($yourVar)
Write-Output $escapedPattern
```
Are you sure the logs show that the special characters are being stripped at the time of using the az CLI command? It could be an issue with how the shell or Azure DevOps handles it. To troubleshoot, try outputting `$(APP_DB_PASSWORD)` in your pipeline before sending it to `az container create`.
Also, have you thought about using the secrets section in Azure DevOps? Storing it there might help, and you’d reference it cleanly. If it shows up correctly in the pipeline, it gives you clues on where the problem lies!
Another option is using the Az Module, or consider making an API call with `Invoke-RestMethod` to create the container directly.
By the way, I typically escape special characters by using a backtick in PowerShell, for example:
$APP_DB_PASSWORD = "wada`"wada^"
Cool, keep us posted on how it goes!
Just a heads-up, if `APP_DB_PASSWORD` is a variable, you should use `$($APP_DB_PASSWORD)` instead of `$(APP_DB_PASSWORD)`, although I see you’re using the right format with others. Not sure if Azure CLI has different requirements but just a thought!
Thanks for pointing it out! I’ve tried multiple formats, but I’ll keep that in mind.
I feel you! It's such a tricky situation.
It definitely sounds like an issue with Azure CLI's handling of the special character substitution. In PowerShell, the command should be:
APP_DB_PASSWORD="wada"wada^"
But Azure CLI might misinterpret that. If you check the [documentation](https://learn.microsoft.com/en-us/cli/azure/use-azure-cli-successfully-shorthand?view=azure-cli-latest#pass-json-in-a-file), it indicates that you could potentially use JSON files for your variables. You could format your variables into a JSON file and then import that in your command:
@{
"APP_DB_PASSWORD" = $APP_DB_PASSWORD
"BLOBSTORAGE_EMAIL_CONTAINER" = $BLOBSTORAGE_EMAIL_CONTAINER
} | ConvertTo-Json | Set-Content ./env.json
az ... --environment-variables ./env.json

Thanks for the suggestion! I'll give that a shot and see if it makes a difference!