【发布时间】:2021-01-20 14:43:01
【问题描述】:
我想使用 Azure DevOps 和 PowerShell 删除 Azure VM 上失败的扩展运行。并根据删除状态要执行另一个 ADO 管道。
【问题讨论】:
标签: azure-devops azure-powershell azure-vm
我想使用 Azure DevOps 和 PowerShell 删除 Azure VM 上失败的扩展运行。并根据删除状态要执行另一个 ADO 管道。
【问题讨论】:
标签: azure-devops azure-powershell azure-vm
您可以添加 azure powershell task 来运行 Az Powershell 脚本来删除扩展。
为了在您的管道中使用 Azure powershel 任务。你需要创建一个 Azure 资源管理器服务连接以连接到你的 Azure 订阅。例如,请参阅this thread。
注意:您需要确保在 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 中调用Runs - 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
【讨论】: