The term 'New-AzureRmApiManagementContext' is not recognized - azure

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.

Related

Get-AzWebApp : The term 'Get-AzWebApp' is not recognized as the name of a cmdlet, function - Azure Devops

I am trying to run below PowerShell script via azure devops pipeline()to add a new Private endpoint to my azure Vnet. Unfortunately I'm getting below error.
Error : Get-AzWebApp : The term 'Get-AzWebApp' 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
azure-pipelines.yml
#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:
- main
pool:
vmImage: 'windows-latest'
steps:
- task: AzureCLI#2
inputs:
azureSubscription: 'Azure subscription 1(XXXXXX)'
scriptType: 'ps'
scriptLocation: 'scriptPath'
scriptPath: 'PrivateEndpointTest.ps1'
PrivateEndpointTest.ps1
$webapp = Get-AzWebApp -ResourceGroupName ENDPOINTTEST -Name anuendpointtest
## Create the private endpoint connection. ##
$pec = #{
Name = 'myConnection'
PrivateLinkServiceId = $webapp.ID
GroupID = 'sites'
}
$privateEndpointConnection = New-AzPrivateLinkServiceConnection #pec
## Place the virtual network you created previously into a variable. ##
$vnet = Get-AzVirtualNetwork -ResourceGroupName 'ENDPOINTTEST' -Name 'VNET_ENDPOINT_TEST'
## Create the private endpoint. ##
$pe = #{
ResourceGroupName = 'VNET_ENDPOINT_TEST'
Name = 'myPrivateEndpoint'
Location = 'North Europe'
Subnet = $vnet.Subnets[0]
PrivateLinkServiceConnection = $privateEndpointConnection
}
New-AzPrivateEndpoint #pe
Please try the Azure PowerShell task and select the latest version.
Azure CLI requires run command below as administrator:
Install-Module Az
Import-Module Az
If you want to use Azure CLI, consider using Self-host agent.
Similar thread for your reference.

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.

Unable to find the JSON file path in Azure DevOps Pipeline which is created for ARM template deployment

I am trying to create Azure resources with ARM template using Azure PowerShell Pipeline.
My two ARM template JSON files are stored in the same directory where the YAML file and Powershell files are stored
Here is the code of the Powershell file
param (
#Name of the Resource Group of the Image Gallery
[Parameter(Mandatory=$true)]
[string]
$deploymentName,
#Name of the Image Gallery
[Parameter(Mandatory=$true)]
[string]
$rgName
)
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $rgName `
-TemplateFile '$(System.DefaultWorkingDirectory)/Deploy.json' `
-TemplateParameterFile '$(System.DefaultWorkingDirectory)/DeployParameters.json'
Here is the code of the 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: none
pool: 'Default'
#vmImage: 'ubuntu-latest'
steps:
- script: echo Create, Shared Image Gallery!
displayName: 'Run a multiline-line script'
- script: |
echo Add other tasks to build, test, and deploy your project.
echo See https://aka.ms/yaml
displayName: 'Run a multi-line script'
- task: AzurePowerShell#5
inputs:
azureSubscription: 'Visual Studio Enterprise Subscription – MPN(08f41212-2053-434e-b4b3-ace08XXXXXX)'
ScriptType: 'FilePath'
ScriptPath: '$(System.DefaultWorkingDirectory)/Deploy_New.ps1'
ScriptArguments: -deploymentName "WVD" -rgName "WVDRG"
azurePowerShellVersion: LatestVersion
pwsh: true
Here is the error I am getting, I am using self-hosted agent pool running in Windows Server 2019 and PowerShell Core.
020-09-15T15:41:38.8359249Z ##[section]Starting: AzurePowerShell
2020-09-15T15:41:38.8615322Z ==============================================================================
2020-09-15T15:41:38.8615818Z Task : Azure PowerShell
2020-09-15T15:41:38.8616217Z Description : Run a PowerShell script within an Azure environment
2020-09-15T15:41:38.8616582Z Version : 5.173.1
2020-09-15T15:41:38.8616919Z Author : Microsoft Corporation
2020-09-15T15:41:38.8617547Z Help : https://aka.ms/azurepowershelltroubleshooting
2020-09-15T15:41:38.8617935Z ==============================================================================
2020-09-15T15:41:40.0811736Z Generating script.
2020-09-15T15:41:40.1352322Z ========================== Starting Command Output ===========================
2020-09-15T15:41:40.1634699Z ##[command]"C:\Program Files\PowerShell\7\pwsh.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'C:\agent\_work\_temp\1d73a74d-12ca-470b-bd53-c23358ee4e2d.ps1'"
2020-09-15T15:41:41.5971906Z Added TLS 1.2 in session.
2020-09-15T15:41:41.7815293Z ##[command]Import-Module -Name C:\Program Files\PowerShell\Modules\Az.Accounts\1.9.3\Az.Accounts.psd1 -Global
2020-09-15T15:41:42.3907347Z ##[command]Clear-AzContext -Scope Process
2020-09-15T15:41:42.7014316Z ##[command]Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue
2020-09-15T15:41:43.3364066Z ##[command]Connect-AzAccount -ServicePrincipal -Tenant *** -Credential System.Management.Automation.PSCredential -Environment AzureCloud #processScope
2020-09-15T15:41:45.2061727Z ##[command] Set-AzContext -SubscriptionId 08f41212-2053-434e-b4b3-XXXXX -TenantId ***
2020-09-15T15:41:47.5198572Z ##[error]Cannot retrieve the dynamic parameters for the cmdlet. Cannot find path 'C:\agent\_work\3\s\$(System.DefaultWorkingDirectory)\DeployHostPool.json' because it does not exist.
2020-09-15T15:41:47.6342141Z ##[error]PowerShell exited with code '1'.
2020-09-15T15:41:48.1235457Z ##[section]Finishing: AzurePowerShell
Can anyone please help here?
You need to swap those ' single quotes for double-quotes, " in New-AzResourceGroupDeployment.
In PowerShell, double quotes will allow for String Expansion which is what you want, in order to use string expansion syntax like this:
"This computer is called $($env:COMPUTERNAME)"
PS>This computer is called eLope
When you use single quotes, it will always give you a string literal, like so:
'This computer is called $($env:COMPUTERNAME)'
PS>This computer is called $($env:COMPUTERNAME)

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

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.

How to stop currently running WebJobs on Azure Pipelines?

I am trying to deploy a new version of WebJobs. I have webjobs.yml file like;
pool:
vmImage: 'ubuntu-latest'
if I try to stop like below, I am receiving error;
- task: InvokeRESTAPI#1
inputs:
connectionType: 'connectedServiceNameARM'
azureServiceConnection: '$(apiName)'
method: 'DELETE'
urlSuffix: 'https://$(apiName).scm.azurewebsites.net/api/triggeredwebjobs/$(apiName)-webjobs'
waitForCompletion: 'true'
WebJobs-API documentation
Step references task InvokeRESTAPI at version 1.152.1 which is not valid for the given job target
And if I use pool: server, as suggested I cannot execute other tasks.
also I tried to use STOP/START like below. It stops AppService but not WebJobs.
- task: AzureAppServiceManage#0
inputs:
azureSubscription: '$(apiName)'
Action: 'Stop Azure App Service'
WebAppName: '$(apiName)'
And I am eventually getting error from Azure:
The process cannot access the file XYZ because it is being used by another process.
So, how I can stop currently running WebJobs from Azure Pipelines, and deploy new version?
There are two solutions here, you can refer to:
The first is you can set this sticky app setting on the staging slot:
WEBJOBS_STOPPED: 1
Set this setting to 1 to disable running any job (will also stop all currently running jobs) Source : Kudu Wiki,case
The second is you can use Invoke-AzureRMResourceAction command. Adding Azure PowerShell task to definition. Source: case1 case2.
Invoke-AzureRmResourceAction -ResourceGroupName XXX -ResourceType Microsoft.Web/sites/ContinuousWebJobs -ResourceName [web app name]/[web job name] -Action [start/stop] -ApiVersion 2015-08-01 -Force
I used inline script in Azure Cli task to update appsettings:
To stop all:
az webapp config appsettings set --name MyAppServiceName --resource-group MyAppServiceResourceGroup --subscription MyAppServiceSubscription --settings WEBJOBS_DISABLE_SCHEDULE=1 WEBJOBS_STOPPED=1

Resources