Next in my few posts about some recent experimentation with using an Azure VM Scale set as a self hosted build agent I wanted to talk about getting the Azure Powershell Task to work.
If you saw my previous posts you would see I had a few experiences getting this working and after I have my agent pool setup and my build is able to run the Azure CLI task I next wanted to check Azure Powershell task was working. I ran a pipeline and got this error.
Could not find the modules: 'Az.Accounts' with Version
You can easily add powershell modules but in the previous articles you will see that I had some hoops to jump through with the interactive aspect of the agent pool and when I am running it in non interactive mode to have the elevated privileges I then get the below different error messages with various attempts to set this up.
Exception calling "ShouldContinue" with "2" argument(s): "Windows PowerShell is in
NonInteractive mode. Read and Prompt functionality is not available
Windows PowerShell is in NonInteractive mode. Read and
Prompt functionality is not available
NuGet provider is required to interact with NuGet-based repositories. Please ensure that
'2.8.5.201' or newer version of NuGet provider is installed
Making it work
Lets go through a step by step to get it working.
Step 1 – Modify / Setup Custom Extension Script for VM Scale Set
What I did to get this working was to modify the custom powershell script I setup for the Azure CLI task to also install some powershell modules. The script now includes these lines:
Install-PackageProvider NuGet -Force
Import-PackageProvider NuGet -Force
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
This will install and import nuget and set the powershell gallery as trusted so I can install modules in my pipeline without having to worry about prompts and permission challenges. I didnt have much luck running these directly in the pipeline which I think will be down to permissions so running them in the custom extension script for the vm scale set start up makes sense because these will be common tasks which are likely to be used by many pipelines who might want to install modules from the Powershell Gallery.
Step 2 – Installing Az Powershell Modules in Pipeline
In the pipeline I should now be able to have a powershell task which will use the below commands to install the Powershell modules. This will take advantage of the work done by the custom extension script which ran when we setup the VM in the scale set to install the nuget provider and to set the PSGallery trust.
Install-Module -Name Az -Force -AllowClobber
Install-Module -Name Az.Accounts -Force -AllowClobber
The yaml for this task is below:
steps:
- powershell: |
Install-Module -Name Az -Force -AllowClobber
Install-Module -Name Az.Accounts -Force -AllowClobber
runScriptInSeparateScope: true
displayName: 'Install Powershell Az Modules'
Step 3 – Check whats installed in Powershell
For diagnostics purposes I wanted to check which modules the pipeline has available. The below script can be put in the normal Powershell task to list the modules and also specifically list the Az.Accounts module so I know its all installed. If this is good I should be able to run the Azure Powershell task now.
#Get all modules installed
Get-InstalledModule
#Check the version of Az.Account installed
Get-InstalledModule Az.Accounts -AllVersions
The yaml for this task is below:
steps:
- powershell: |
#Get all modules installed
Get-InstalledModule
#Check the version of Az.Account installed
Get-InstalledModule Az.Accounts -AllVersions
displayName: 'Check Powershell'
Step 4 – Test the Azure Powershell Task
Next I can put an Azure Powershell task in the pipeline and just do something simple like getting a resource group to check I can connect to Azure via Powershell and do something. The below script is what I used.
Get-AzResourceGroup -Name "Blog_BuildAgent"
The yaml for this task is below:
steps:
- task: AzurePowerShell@5
displayName: 'Azure PowerShell - Test Azure Connection'
inputs:
azureSubscription: '[Subscription]'
ScriptType: InlineScript
Inline: |
# Quick Test to check Azure Powershell is working
Get-AzResourceGroup -Name "Blog_BuildAgent"
azurePowerShellVersion: LatestVersion
continueOnError: true
Summary
Hopefully this shows a pretty simple way to use the custom extension script to setup Powershell to be able to work with the Az modules when its running on a more locked down self hosted vm as part of an Azure VM scale set.