如何解决如何使用 Azure DevOps 和 PowerShell 删除 Azure VM 上失败的扩展运行?
我想使用 Azure DevOps 和 PowerShell 删除 Azure VM 上失败的扩展运行。并根据删除状态想要执行另一个ADO管道。
解决方法
您可以添加 azure powershell task 以运行 Az Powershell 脚本以删除扩展。
为了在您的管道中使用 Azure powershel 任务。您需要创建 Azure 资源管理器服务连接以连接到您的 azure 订阅。例如,请参见 this thread。
注意:您需要确保您在 Azure Resource Manager 服务连接中使用的服务主体具有正确的角色分配以删除 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。