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.
Related
I create Azure VM (windows) via arm template from Azure Pipeline. But I have to Add New Property Item to Registry without RDP.
Is there a secure way to do that with ARM template or SDK?
Any advice would be appreciated.
Best
You could do that with many different ways. One method is to perform an ARM deployment and use commandToExecute inside it. You could also perform that using a Powershell task without having to login in the VM through Azure portal using az cli.
An example can be found below:
- task: AzureCLI#2
displayName: execute command inside vm
inputs:
azureSubscription: 'subscription'
scriptType: 'ps'
scriptLocation: 'inlineScript'
inlineScript: 'az vm run-command invoke --command-id RunPowerShellScript --name $(vm_name) -g $(vnet_rg_name) --scripts "hostname"'
In the scripts section instead of the hostname you should add a powershell to edit your registry key.
Documented article:
https://medium.com/#geralexgr/execute-powershell-command-without-username-password-on-azure-virtual-machine-8142ade31fd0
I am using let's encrypt certificate and azure key vault to automate renewal process using this repo: https://github.com/brent-robinson/posh-acme-azure-example
I have installed the module Az.KeyVault using yaml on azure devops pipeline:
# Install the Az PowerShell modules our script will need
- task: PowerShell#2
displayName: Install PowerShell Modules (Az.Accounts, Az.KeyVault, Az.Resources, Posh-ACME)
inputs:
targetType: 'inline'
script: 'Install-Module Az.Accounts, Az.KeyVault, Az.Resources, Posh-ACME -Force'
errorActionPreference: 'stop'
failOnStderr: true
pwsh: true
But, when I run the script, getting the below error:
The 'Get-AzKeyVaultCertificate' command was found in the
| module 'Az.KeyVault', but the module could not be loaded. For
| more information, run 'Import-Module Az.KeyVault'.
When I try to add the import module (Import-Module -Name Az.KeyVault -Force) it's giving the below error:
Assembly with the same name is already loaded
There is an issue with the latest version of Posh-ACME module (4.15.0).
Install version 4.14.0 to resolve this.
Install-Module -Name Posh-ACME -RequiredVersion 4.14.0 -Force
Ref: https://github.com/brent-robinson/posh-acme-azure-example/issues/15#issuecomment-1241850416
When you try to install the module in CI/CD pipeline you can specify the user so that it won't conflict with other.
#install Az PowerShell Modules with Specific User
- task: PowerShell#2
displayName: Install PowerShell Modules (Az.Accounts, Az.KeyVault, Az.Resources, Posh-ACME)
inputs:
targetType: 'inline'
script: 'Install-Module Az.Accounts, Az.KeyVault, Az.Resources, Posh-ACME -Force CurrentUser'
The 'Get-AzKeyVaultCertificate' command was found in the
| module 'Az.KeyVault', but the module could not be loaded. For
| more information, run 'Import-Module Az.KeyVault'.
Assembly with the same name is already loaded
The error messages show that Az keyVault was already installed but not able to load in a pipeline to run task. You can specify the user(Current user or all user) to install the required module to specific user and import the required module.
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.
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.
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