Use Powershell in Azure DevOps YAML pipelines.
Scripts act as the fuel for your DevOps pipelines and PowerShell plays a major role in automating your tasks. I recently worked on an Azure DevOps project where a lot of automation was scripted using PowerShell. There are many ways to run PowerShell tasks in your YAML pipelines and I will try to explain most of them
PowerShell is a task automation and configuration management program from Microsoft, consisting of a command-line shell and the associated scripting language
Inline scripts
You can run your PowerShell scripts as inline scripts in your YAML files, either as a single line or multi-line
- Powershell: "PowerShell single line"
- Powershell: |
This is script line 1
This is script line 2
Below are the various method to run your inline PowerShell scripts
Method 1: Using powershell.exe
powershell.exe
is the executable name of the legacy Windows PowerShell edition (v5.1-), built on the Windows-only .NET Framework (v4.8-) It doesn't support cross-platform execution and only on windows agents.
stages:
- stage: Initialize
displayName: 'Initialize Stage'
pool: "windows-server-pool"
jobs:
- job: Install
displayName: 'Install Dependencies (PowerShell)'
steps:
- script: |
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "Install-PackageProvider NuGet -Scope CurrentUser -Force"
displayName: 'Powershell Install NuGet'
- script: |
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "Install-Module -Name SqlServer -AllowClobber -Scope CurrentUser -Force"
displayName: 'Powershell Install SqlServer Module'
Method 2: Using pwsh.exe
pwsh[.exe]
is the executable file name of PowerShell [Core] (v6+), the cross-platform edition of PowerShell built on .NET Core / .NET 5+
- pwsh: './SmokeTest.ps1 -url $(WebAppDeploy.AppServiceApplicationUrl)'
displayName: 'Run Smoke Test'
workingDirectory: '$(Pipeline.Workspace)/DeployScripts'
failOnStderr: true
pwsh
runs PowerShell Core, which must be installed on the Linux specific agent or container and to install pwsh in your Linux agents, run
sudo apt-get install -y PowerShell
pwsh --version
Method 3: — PowerShell task
- powershell: |
Install-Module -Name Az -AllowClobber -Scope CurrentUser -Force
displayName: 'Install Azure Powershell'
Method 4: PowerShell@2 Task
This is the shortcut of `- powershell:` and ` — pwsh` and depending on the options chosen, the pipeline agent will either be on Windows or Linux.
- task: PowerShell@2
displayName: 'Run Smoke Test'
inputs:
filePath: '$(Pipeline.Workspace)/DeployScripts/SmokeTest.ps1'
arguments: '-url $(WebAppDeploy.AppServiceApplicationUrl)'
failOnStderr: true
pwsh: true
The advantage of using inline code is keeping all functionality in a single place, making it easier to see everything that’s going on. But, this approach can get complicated if you have a large pipeline.
Scripts
You can run .ps1 scripts also in your YAML pipelines. Save the .ps1 files inside a folder under $(System.DefaultWorkingDirectory) and set targetType
to filePath
for execution. Then specify the path of the script to run via the filePath
attribute.
- task: PowerShell@2
retryCountOnTaskFailure: 2
inputs:
targetType: 'filePath'
filePath: $(System.DefaultWorkingDirectory)/Scripts/health.ps1
arguments: > # Use this to avoid newline characters in a multiline string
-uri "https://myapp.azurewebsites.net/health"
displayName: 'Health check'
By default, Azure DevOps will checkout all code in the source repo. Checking out the code will download script files from the repo onto the pipeline agent, making them available for execution. The exception here is the `- deployment:` job which you will have to force checkout using — checkout: self
Conclusion
PowerShell is so handy for the DevOps engineers, which works on both Windows and Linux agents. Azure DevOps supports both PowerShell inline and scripts inside your pipelines, which can be chosen based on the complexity of your code.
Contact us today to schedule a cloud workshop to learn more about how we can assist you in planning your Azure migration