Azure DevOps is a powerful tool for any organization. Thanks to Azure DevOps you can perform a lot of admin tasks. For instance, you can quickly deploy applications to various Azure services such as Virtual Machines.
Sometimes, in the Release Pipelines, you might need to add PowerShell Tasks. In these PowerShell Tasks, you might also need to pass arguments to your script, so in this article, I will describe how to pass arguments in Release Azure DevOps PowerShell Tasks.
Deploying Azure DevOps Pipeline
First, we have to create a sample PowerShell script that will be stored in our Azure DevOps repository. This script will be used in the release task as “PowerShell Task”.
This script is a simple script that will create a Confluence space using Atlassian REST API, but you can create a script to perform any task you want.
The important thing to do is to include the following code at the beginning of your script:
Thanks to the CmdletBinding, you will be able to add [Parameter()] decorators to parameters (more information here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_cmdletbindingattribute?view=powershell-7.1)
At this step, you must confirm that your script runs smoothly locally. Then, you can create your pipeline and add the variables you need. Go to the Variables tab and click Add
In my case, I need to create 5 variables because I need to pass 5 arguments to the script. The name of the variables can be the same between your script and the pipeline but it is not mandatory. Now, go to “Tasks” and click “+”, then search “PowerShell” and click “Add”
Browse the repository and select your PowerShell script. In the arguments field, add the following code:
-ScriptArg1 $(PipelineArg1) -ScriptArg2 $(PipelineArg2) -ScriptArg3 $(PipelineArg3) -ScriptArg4 $(PipelineArg4) -ScriptArg5 $(PipelineArg5)
- “ScriptArg1” is the argument you write at the beginning of your script
- “PipelineArg1” is the variable you create in the pipeline
Once it’s ok, you can save and create the release.
Wait until the release has been created.
Click Deploy to run the task and confirm that the variables are passed to the script.
To confirm the result, go to Logs and check the status.