Passing boolean parameters to PowerShell scripts in Azure DevOps Pipeline
If you need to call PowerShell scripts form Azure DevOps pipeline you need to pay attention to boolean parameters.
Let’s start from the problem, I have an Azure DevOps pipeline that calls a PowerShell script and the team needs to change the pipeline allowing a boolean parameter to be passed to the PowerShell script when you queue the pipeline. The first tentative produces this error:
|
|
The original code of the pipeline is the following one.
|
|
This is a simple trick I taught to people, create a Parameter (it allows you to specify the value at queue time), then create a variable that reference the parameter so everything in the pipeline is referenced with $(variableName) syntax. But in this situation we have a problem, a boolean variable will contain real true/false value but when you convert to a variable it will become a string. If you pass variable value to a PowerShell boolean value it will throw error because it expects $true or $false.
PowerShell boolean values can be converted from normal string containing $true or $false
The solution is simple, just prepend $ character to the variable
|
|
As you can see in line 8 we have a double dollar sign, the first one is a standard Dollar char the second one is used to reference variable ForceInstallPackage. This trick is used to pass the correct value to PowerShell because it will prepend dollar sign to string true/false variable value.
Now we can just queue the pipeline for another run.
Figure 1: Parameter is available at queue time
As you can see Force installation of PowerShell build utils module is rendered as a checkbox (it is a boolean parameter) but it is passed to PowerShell script correctly.
Gian Maria.