Menu Close

Pass boolean values to a PowerShell script in Azure DevOps pipelines

Passing through parameters in YAML pipelines in Azure DevOps works perfectly fine. However, when passing these parameters to a PowerShell script you might notice some strange behaviour. Parameter values are casted in the same type on both sides, but still get mixed up.

That was exactly what I was running into when working on YAML pipelines in Azure DevOps: misinterpreted parameter values. It took me a bit of research on what was happening. I’m sharing that knowledge here for your convenience. Because if you know, you know.

The situation

As mentioned I was working on a YAML pipeline in Azure DevOps. For context: it was a pipeline to delete resources, which comes in handy when you want to redeploy an entire solution. From the YAML pipeline a PowerShell script is called to actually delete the resources from Azure. Because we may not want to delete everything a boolean is added to check whether or not to delete the entire Resource Group as well:

When calling the script from my local machine everything works as expected. But as soon as I added the script to my YAML pipeline in Azure DevOps, where I used the pipeline’s parameters as input for the script, I noticed errors while testing:

##[error]Cannot process argument transformation on parameter 'RemoveIntegrationResourceGroup". Cannot convert value "System.String" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.

So I checked my YAML pipeline again, and saw the parameter is casted as boolean value. I’m passing the value through one-on-one, without any mutations whatsoever:

And in my PowerShell script the parameters are casted as boolean values as well:

Still PowerShell thinks I’m passing a string where it expects a boolean. Strange…

The solution

I’ve found multiple solutions around this issue, but it turned out to be quite simple: just add a dollar sign ($) before the parameter value you want to pass through as boolean. You end up with a double dollar sign notation, just like below:

Even though it might seem strange, it works like a charm! But why?

The reason

When knowing the solution to this situation, it still feels a bit odd. Why would we want to have a double dollar sign when passing through boolean values? It doesn’t seem to add up.

However, when you look at the way booleans are noted in a YAML file it’s basically just the text “true” (or “false”, off course). And that is the exact reason PowerShell throws an error: PowerShell doesn’t expect a boolean to be text, but a real true/false value. So the YAML pipeline is passing through this:

true

While PowerShell expects a value like this:

$true

So your parameter syntax in YAML simply passes through the value as-is, YAML style. To convert it to PowerShell you need to add an additional dollar sign ($), resulting in a double dollar sign notation. This makes the YAML parameter value, being the text ‘”true”, passed on as the PowerShell boolean value “$true”.

Share your thoughts