AzurePowerShell#4 - powershell commands fail to execute on azure pipeline - azure

As a part of my yml pipeline definition, I have a AzurePowerShell#4 task, following is an extract from my pipeline definition
stages:
- stage: DeployDemoCluster
jobs:
- job: 'DeployAKSAndAll'
pool:
vmImage: 'windows-latest'
steps:
- task: AzurePowerShell#4
displayName: Store AI instrumentation key for Inbound Processor in central KeyVault
inputs:
azureSubscription: 'service-connection'
azurePowerShellVersion: LatestVersion
pwsh: true
ScriptType: 'FilePath'
ScriptPath: 'AKS/ps/update_kv_firewall.ps1'
The issue is, within my update_kv_firewall.ps1, all the powershell commands fail with the error, for example:
[error]Login-AzureRmAccount : The term 'Login-AzureRmAccount' is not recognized as the name of a cmdlet, function, script file, or operable program.
The script when executed individually / standalone, works perfectly fine.
what am I missing here?

As per your comment: the command "Get-AzKeyVault" runs without any errors, while 'Get-AzureRmVirtualNetwork' leads to errors.
Then I'm sure that you're installing the new Az module of azure powershell. So the command like Get-AzKeyVault can work.
Since you're installing Az module, please use all the commands from Az module. Almost each azure Rm command has an equivalent azure Az command, you can find it from the Az command list.
Note: the command like Get-AzureRmVirtualNetwork / Login-AzureRmAccount is from azure RM module, which will be retired this year later.

Related

Azure DevOps & Azure CLI: Unable to locate executable file

When trying to run a simple Azure CLI Task in my ubuntu-based Azure DevOps pipeline I get the following error message:
##[error]Script failed with error: Error: Unable to locate executable file:
'/home/vsts/work/_temp/azureclitaskscript1637831708745.bat'.
Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable.
Also check the file mode to verify the file is executable.
If I'm reading this correctly the inline script is not being found, right? What am I missing here? Here's the full YAML:
trigger:
- main
- dev
pool:
vmImage: ubuntu-latest
steps:
- task: AzureCLI#2
inputs:
azureSubscription: 'My Subscription Name'
scriptType: 'batch'
scriptLocation: 'inlineScript'
inlineScript: 'az --version'
You're running your task on Linux, you can't use batch there, you'll need to pick pscore or bash instead, or switch to a windows-latest vmImage.
Script Type*: Select the type of script to be executed on the agent. Task supports four types: Batch / Shell / PowerShell / PowerShell Core scripts, default selection being empty. Select Shell/PowerShell Core script when running on Linux agent or Batch/PowerShell/PowerShell Core script when running on Windows agent. PowerShell Core script can run on cross-platform agents (Linux, macOS, or Windows)
https://github.com/microsoft/azure-pipelines-tasks/tree/master/Tasks/AzureCLIV2#parameters-of-the-task
The actual strings to pass to the arguments can be found in the task.json. It looks like the task doesn't do any validation on these fields, causing this weird error situation.
"options": {
"ps": "PowerShell",
"pscore": "PowerShell Core",
"batch": "Batch",
"bash": "Shell"
}

AzurePowerShell task cannot find filePath

I'm trying to run a PowerShell script in Azure Yaml Pipelines and I'm getting this error:
##[error]The term 'D:\a\1\s\myPowershellFile.ps1' 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.
Code:
jobs:
- deployment: Deploy
displayName: Deploy
environment: $(myEnvironment)
pool:
vmImage: 'windows-latest'
strategy:
runOnce:
deploy:
steps:
- task: AzurePowerShell#5
displayName: 'Run Powershell script'
inputs:
azureSubscription: $(azureConnectionName)
scriptType: filePath
scriptPath: './myPowershellFile.ps1'
azurePowerShellVersion: latestVersion
The file is pushed out to the repo for the branch that is triggering the build. I've also tried referencing the path explicitly with $(Pipeline.Workspace) and $(Build.SourcesDirectory). Version 4 also does not work. According to the docs this should be working!
After some more research I discovered this article which says that files are not automatically downloaded for deployment jobs. I added this step, and that fixed the issue!
- checkout: self
Couldn't help noticing but you have used the forward slash "/" as the seperator for the filepath instead of a back-slash "\" like in the example you are pointing to in the azure docs :
- task: AzurePowerShell#5
inputs:
azureSubscription: my-arm-service-connection
scriptType: filePath
scriptPath: $(Build.SourcesDirectory)\myscript.ps1
This looks like the reason why.
There is a similar issue here. looks like it depends on whether you are using a windows or linux agent : cannot locate file in azure devops pipeline

The term 'New-AzureRmApiManagementContext' is not recognized

I am trying to create a context of Azure API management through scripts. The scripts I have I am able to execute the inbulit powershell terminal in Azure portal. But I am trying to run that scripts through my pipeline. currents pipeline runs under ubuntu, here is the yaml task. I have tried with enable core as well as without it.
steps:
- task: AzurePowerShell#5
displayName: 'Azure PowerShell script: InlineScript'
inputs:
azureSubscription: '<sc>'
ScriptType: InlineScript
Inline: |
Set-AzContext -SubscriptionId $(subId)
$apimContext = New-AzureRmApiManagementContext -ResourceGroupName "$(rgName)" -ServiceName "$(serviceName)"
pipeline throws standard error -
The term 'New-AzureRmApiManagementContext' is not recognized
| as a name of a cmdlet, function, script file, or executable
| program. Check the spelling of the name, or if a path was
| included, verify that the path is correct and try again.
Is there something I need to import while running this?
The workaround, I can see there is a migration on the powershell happening
https://learn.microsoft.com/en-us/powershell/azure/migrate-from-azurerm-to-az?view=azps-6.5.0.
So If I change that from New-AzureRmApiManagementContext to New-AzApiManagementContext in the pipeline it works.

Azure DevOps Parameters not recognised by AZ CLI

I am having issues passing parameters defined in an Azure Pipeline YAML to AZ Cli located in a bash script. I found the following solution on Stackoverflow but it doesn't seem to work:
Pass parameter to Azure CLI Task in DevOps
Here is my YAML file:
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
steps:
- task: AzureCLI#2
displayName: Azure CLI
inputs:
azureSubscription: templateresourceconnection
scriptType: bash
scriptPath: ./deployment.sh
arguments:
-resourceGrp 'TEST-RG'
-location 'westeurope'
I would expect to be able to access the arguments in my deployment.sh, which fails:
#!/bin/bash
# Create Resource Group
az group create -n $(resourceGrp) -l $(location)
If I don't pass any arguments and hardcode the values in deployment.sh it all works fine.
Any ideas what could cause this issue? I also tried with UPPERCASE and just without brackets.
I get the following error message
Do you have any idea what else I could try to make it work. Seems like the documentation doesn't contain any example for az cli. Just how to define parameters but not how to pass them afterwards.
Thank you.
Do you have any idea what else I could try to make it work
You could try to use the Environment variable. Environment variables can be accessed by bash script files.
First of all, you need to define pipeline variable instead of task argument.
variables:
- name: one
value: initialValue
Here is my example: Used in Linux system.
az group create -n $RESOURCEGRP -l $LOCATION
Note: All characters in environment variables need to be capitalized.
e.g.: resourceGrp -> $RESOURCEGRP
Yaml sample:
variables:
- name: resourceGrp
value: TEST-RG
- name: location
value: westeurope
pool:
vmImage: 'ubuntu-latest'
steps:
- task: AzureCLI#2
displayName: 'Azure CLI deploy.sh'
inputs:
azureSubscription: kevin0209
scriptType: bash
scriptPath: ./deployment.sh

AzureRM commands in Azure powershell Task: Azure DevOps

I am trying to run a script in the Azure Powershell Task in Azure DevOps that uses AzureRM commands. I tried to use Task version 4.* and 5.*. Both of them are giving error saying that the AzureRM commands couldnt be recognized. (eg. Get-AzureRmResourceGroup is not recognized). What should I do to solve this issue?
You have there Az Modules available not AzureRM so if you try this
- task: AzurePowerShell#5
continueOnError: true
inputs:
azureSubscription: 'rg-the-code-manual'
ScriptType: 'InlineScript'
Inline: 'Get-AzResourceGroup -Name "TheCodeManual"'
azurePowerShellVersion: LatestVersion
You should get your result.
And if you want you may try to uninstall Az modules and then install AzureRm module. Here you have example how to uninstall az modules.
Here is a simple solution:
You may also take a look here to see how to force installation.

Resources