如何使用Azure DevOps和PowerShell删除Azure VM上运行失败的扩展?



我想使用Azure DevOps和PowerShell删除Azure VM上失败的扩展运行。并根据删除状态想要执行另一个ADO管道。

您可以添加一个azure powershell任务来运行Az powershell脚本来删除扩展。

为了在管道中使用Azure powershell任务。您需要创建Azure资源管理器服务连接以连接到Azure订阅。请看这个线程的例子。

注意:您需要确保在Azure资源管理器服务连接中使用的服务主体具有正确的角色分配以删除vm扩展。

然后你可以运行下面的az powershell命令来检查你的扩展的状态并删除它们。

  • 获取虚拟机上安装的所有扩展:

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

  • 获取扩展的属性:

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

  • 从虚拟机中删除扩展名:

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

根据删除状态触发另一个ADO管道。你可以在上面的azure powershell中调用Run - Run Pipeline rest api来触发另一个管道。请看下面的例子:

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

最新更新