Troubleshooting common problems in Azure Devops
I love orchestration and automation, believe me I do. But there is just something about understanding the vagaries and internal inconsistencies of these products that could break a lesser System Admin or Devops guru.
I love orchestration and automation, believe me I do. But there is just something about understanding the vagaries and internal inconsistencies of these products that could break a lesser System Admin or Devops guru.
For one thing, who wants to waste fifteen minutes reading the docs when they could take two whole days figuring it out instead?!
In this short post, Iâll help you solve a few small problems you might encounter trying to use Azure PowerShell commands in your Azure Devops Pipeline.
Topics Covered
-
Could not find the modules Az.Accounts
-
##[error] The term âC:__w\1\sâ is not recognized as a name of a cmdlet, function
-
403 Forbidden error on using Azure PowerShell commands
-
No output when running
AzurePowerShell@
commands in Azure Devops pipelines.
If the module was recently installed, retry after restarting the Azure Pipelines task agent. ##[error]PowerShell exited with code â1â.
When you use the AzurePowerShell
or other Azure*
tasks in Azure Devops, you might expect that they will setup your modules for you. This is not in fact the case. What they do do for you is run Initialize-Azure
for you before your command executes, which handles setting up your Azure credentials from your Service Connection, SPN, or MSI, and then runs Connect-AzAccount
and Set-AzContect
.
This means that before you can call any Azure*
commands, you should first setup your PowerShell modules manually, using this command.
- task: PowerShell@2
displayName: 'Update Az.Accounts Module'
inputs:
targetType: 'inline'
script: |
Get-PackageProvider -Name NuGet -ForceBootstrap
Install-Module -Name Az.Accounts -Verbose -Force -AllowClobber
Uninstall-AzureRm -Verbose
After this, the AzurePowerShell
commands can work.
The term âC:__w\1\sâ is not recognized as a name of a cmdlet, functio
Letâs say you setup your modules as above, and then go to run subsequent commands, like this one:
- task: PowerShell@2
displayName: 'Update Az.Accounts Module'
inputs:
script: |
Write-host "Hello world"
Seems super simple right? But then you see this lovely error:
##[error]The term 'C:\__w\1\s' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
This occurs because the PowerShell@2
command has an optional parameter of targetType
, with the supported values of inline
and script
. The default value is script
. This means that Azure Devops is interpretting our Write-host...
as the path to a script file. Notice the w
letter? I know itâs soooo dumb.
Fix
If youâre running some inline commands as in my example, always add this param:
- task: PowerShell@2
displayName: 'Update Az.Accounts Module'
inputs:
targetType: 'inline' <============ HERE
script: |
Write-host "Hello world"
For the love of God, so help me if you include the <======== HERE
bit in your code then come crying to me like
âFoxDeploy-san, help me!! It says
<=========== HERE
is not a known PowerShell command QQâ
We are going to have words.
No output when running an AzureDevops command
How about this! Your command executes without an error and nothing happens.
For instance, you might see this
2024-05-10T15:59:13.3300799Z [33;1mVERBOSE: Command [Set-AzContext] succeeded.[0m
2024-05-10T15:59:14.8584021Z ##[command]Disconnect-AzAccount -Scope CurrentUser -ErrorAction Stop
How lovely, it finishes executing Set-AzContext
and then immediatly runs Disconnect-AzAccount
. What the shell
Well in a lovely turn of events, if you happen to use AzurePowerShell
thinking that âSurely this will work and be the same as PowerShell@2â then you will be greeted with Pain.
WTF is it this time?
Specifically when using an inline script you must set your ScriptType
to 'InlineScript'
, and then provide your actual script in Inline: |
format.
The below example is known good and will work and save you much frustration.
- task: AzurePowerShell@3
displayName: "Create Resource Group with version 3"
inputs:
azureSubscription: "putYourOwnServiceConnectionNameHere"
azurePowerShellVersion: latestVersion
ScriptType: 'InlineScript'
Inline: |
$dateTime = Get-Date
$formattedString = "FoxTest{0:MMddHHmm}v3" -f $dateTime
new-azresourcegroup -location eastus $formattedString -verbose
Write-output $formattedString
pwsh: true
The format of this command is noticibly different from a plain PowerShell@2
command, so if you get no output from your AzurePowerShell
commands, be sure to check this!
I get a 403 Forbidden
Nothing like getting all the way here, to actually running your command and then you experience an error.
Well, remember my golden rule âEvery new failure message gets you closer to successâ.
Actually thatâs my silver rule, the true golden rule is:
Well, if youâre using a Service Connection to authenticate to Azure in your pipelines(and you should be), you may have expected from the documents that your account would automatically be granted permissions to the relevant subscription.
One thing Iâve found to check is to ensure your Service Connection account was actually granted permission to the target subscription.
In my case, my Azure operations were failing due to insufficient permissions, but once I manually applied Contributor permissions to my new Enterprise Application, the task completed on next operation!