Thursday, 19 December 2019

Resolving Issues with | Symbol in PowerShell While Creating Node Web App in Azure CLI

You can use Azure Command Line Interface with PowerShell to create infrastructure as code (IaC) scripts, for implementing deployment of resources in Azure platform services. When you try to create a web app in Azure with run time specified with Azure CLI in PowerShell, | symbol used in runtime specification causes issues as it is trying to do a pipe operation in PowerShell. Let’s look at the issue and how to get it resolved in PowerShell and Azure CLI based IaC scripts.
The Issue
When the script below executed, $nodeRuntime is set as node|6.12 the script fail with issue shown in below image.
$webAppPricePlan = az appservice plan show -n $azureFunctionsAppPlanName -g $sharedResourceGroupName | ConvertFrom-Json
Write-Host ("Web App " + $name + " is not found. Creating it...")
az webapp create -n $name -g $resourceGroupName -p ($webAppPricePlan.id) -r $nodeRuntime
'6.12' is not recognized as an internal or external command, operable program or batch file.

How to Resolve
To resolve this issue, we can use --% as escape to | symbol. However, to run this in a PowerShell script with variables, we have to copy it as an evaluated expression. The execute it via Invoke-Expression. If the script is getting executed with hardcoded values instead of variable usage you do not have to use Invoke-Expression.
$exp = 'az webapp create -n ' + $name + ' -g ' + $resourceGroupName + ' -p ' + ($webAppPricePlan.id) + ' --% -r ' + '"' + $nodeRuntime + '"'
Invoke-Expression($exp)

Once this is resolved the script can be used in Azure Pipelines to create node web app resources in Azure.

No comments:

Popular Posts