1

I want to delete the failed runs of extensions on Azure VM using Azure DevOps and PowerShell. And based on the deletion status want to execute the another ADO pipeline.

Yogesh Kulkarni
  • 339
  • 6
  • 24

1 Answers1

1

You can add an azure powershell task to run Az Powershell scripts to delete extensions.

In order to use Azure powershel task in your pipeline. You need to create an Azure Resource Manager service connection to connect to your azure subscription. See this thread for example.

Note: You need to make sure the service principal you used in Azure Resource Manager service connection has the proper role assignment to delete the vm extension.

Then you can run below az powershell commands to check your extensions' states and delete them.

  • Get all extensions installed on a VM:

    Get-AzVMExtension -ResourceGroupName "ResourceGroup11" -VMName "VirtualMachine22"

  • Get properties of an extension:

    Get-AzVMExtension -ResourceGroupName "ResourceGroup11" -VMName "VirtualMachine22" -Name "CustomScriptExtension"

  • Remove an extension from a virtual machine:

    Remove-AzVMExtension -ResourceGroupName "ResourceGroup11" -Name "ContosoTest" -VMName "VirtualMachine22"

To trigger another ADO pipeline based on the deletion status. You can call the Runs - Run Pipeline rest api in above azure powershell to trigger another pipeline. See below example:

steps:
- task: AzurePowerShell@5
  displayName: 'Azure PowerShell script: InlineScript copy'
  inputs:
    azureSubscription: 'Microsoft-Azure'
    ScriptType: InlineScript
    Inline: |
     #remove extension
     $result = Remove-AzVMExtension -ResourceGroupName "ResourceGroup11" -Name "ContosoTest" -VMName "VirtualMachine22"
     
     if($result.IsSuccessStatusCode){
           
          $url = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/pipelines/{pipelineId}/runs?api-version=6.1-preview.1"
          #invoke rest api to trigger another ado pipeline
          Invoke-RestMethod -Uri $url -Headers @{authorization = "Bearer $(system.accesstoken)"} -ContentType "application/json" -Method Post
        
     }
     
    azurePowerShellVersion: LatestVersion
Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43