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.
1 Answers
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

- 27,483
- 2
- 31
- 43
-
Thanks Levi. I will try this and update the thread. – Yogesh Kulkarni Jan 21 '21 at 06:25
-
@YogeshKulkarni How did it going with above scripts? – Levi Lu-MSFT Jan 26 '21 at 08:58
-
Hi Levi, removal of extension worked. Thanks a lot. – Yogesh Kulkarni Jan 27 '21 at 15:26