I am trying to run a Powershell script in Azure DevOps pipelines.
Since the script uses Az
module commands, we decided to use AzurePowerShell@5
task in the pipeline.
As mentioned in this doc:
By default, Azure PowerShell v5 uses PowerShell Core for Linux agents
Since we have self-hosted Linux agents, it is necessary to manually install PowerShell Core and Az modules on the agent before using AzurePowerShell@5
.
Followed these steps to install powershell core and the steps mentioned here to install Az
module.
# Install powershell core
sudo apt-get update
sudo apt-get install -y wget apt-transport-https software-properties-common
wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb"
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update
sudo apt-get install -y powershell
# Install Az Module
Install-Module -Name Az -Repository PSGallery -RequiredVersion '11.1.0' -Force
Update-Module -Name Az -Force
But when we use the task in the pipeline,
- task: AzurePowerShell@5
name: test_script
displayName: 'test_script'
inputs:
azureSubscription: $(arm_service_connection)
ScriptType: 'FilePath'
ScriptPath: $(System.DefaultWorkingDirectory)/powershell_scripts/test_script.ps1
ScriptArguments: >
-subscriptionId $(subscription_id)
azurePowerShellVersion: 'OtherVersion'
preferredAzurePowerShellVersion: '11.1.0'
FailOnStandardError: true
pwsh: true
it gives an error:
Generating script.
/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command . '/agent/_work/_temp/f3210d6a-92eb-4665-92bd-9c6790840726.ps1'
File saved!
Exception: /agent/_work/_tasks/AzurePowerShell_72a1931b-effb-4d2e-8fd8-f8472a07cb62/5.231.0/Utility.ps1:94
Line |
94 | … throw ("Could not find the module path with given version …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Could not find the module path with given version.
##[error]PowerShell exited with code '1'.
##[error]PowerShell wrote one or more lines to the standard error stream.
How to fix this?
I found a blog here that explains the same. I also checked the file that was mentioned in the error.
It is evident that the AzurePowerShell@5
task is looking for the Az
module files at a path /usr/share/az_<azurePowerShellVersion>
.
So, I installed the module in the correct path using the following script and it worked:
# Install powershell core
sudo apt-get update
sudo apt-get install -y wget apt-transport-https software-properties-common
wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb"
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update
sudo apt-get install -y powershell
# Install Az Module in correct path
sudo mkdir -p /usr/share/az_11.1.0
sudo /usr/bin/pwsh -Command 'Find-Module -Name Az -RequiredVersion '11.1.0' -Repository 'PSGallery' | Save-Module -Path '/usr/share/az_11.1.0' -Force -Verbose'
Make sure the ado agent user has read
and execute
permissions on that module directory and files.
Specifying these parameters for the task is important to pick up the correct version:
azurePowerShellVersion: 'OtherVersion'
preferredAzurePowerShellVersion: '11.1.0'