【问题标题】:Remove/Delete all resources created while spinning a new VM through powershell删除/删除通过 powershell 旋转新 VM 时创建的所有资源
【发布时间】:2019-02-09 01:22:34
【问题描述】:

我已经使用 template.json 文件通过 powershell 创建了一个虚拟机。这些是创建的资源类型列表。
1. 虚拟机
2. 网络接口
3. 网络安全组
4.磁盘

现在,我要做的是在新创建的 VM 上更新一些应用程序,概括 VM 并创建新 VM 的映像。到这里我可以通过powershell做到这一点。

现在我的要求是删除在旋转 VM 期间创建的所有资源。我知道删除 VM 的 cmdlet 就是这个。

Remove-VM -Name "new 2" -Force

是否有任何单个命令可以执行此任务?

非常感谢任何帮助或输入。谢谢

【问题讨论】:

    标签: azure powershell azure-virtual-machine azure-powershell


    【解决方案1】:

    当您删除 VM 时,与该 VM 关联的资源不会自动删除,主要是这样您可以在需要时重新使用它们,例如,如果您正在删除一个 VM,但想用相同的方法重新创建它VHD 等。如果您知道要删除 VM 和相关资源,这可能会很烦人。除了手动删除所有内容之外,您实际上只有 2 个选项

    1. 删除资源组 - 这有助于将资源组的范围尽可能小,以帮助实现这一目标

    2. 为你删除虚拟机和资源的脚本,一个很好的例子是here

    我建议您对另一个 Azure 客户提交的想法进行投票。

    https://feedback.azure.com/forums/216843-virtual-machines/suggestions/8945692-delete-vm-with-all-associated-resources

    您在这些论坛中分享的所有反馈都将由 Azure 团队监控和审查。

    【讨论】:

      【解决方案2】:

      如果您将 VM 和所有相关资源关联到单个资源组,则只需删除该资源组即可删除所有附加资源及其自身的 VM。

      你会像 tat 那样做:

      Get-AzResourceGroup -Name "ContosoRG01" | Remove-AzResourceGroup -Force
      

      这里还有一篇完整的博客文章,描述了如何使用 AzureRM 和 powershell 编写 VM 创建脚本。

      Create a Windows Server virtual machine with PowerShell

      【讨论】:

        【解决方案3】:

        我在下面的链接中准备了一个脚本。只需输入 VM 名称,脚本即可处理其余部分 http://cloudcompute.info/delete-azure-vm-and-all-associated-resources-using-powershell-script/

        这是完整的脚本

        Write-Host -NoNewline -ForegroundColor Green "Please enter the VM name you would like to remove:"
        $VMName = Read-Host
        $vm = Get-AzVm -Name $VMName
        $RGName=$vm.ResourceGroupName
        Write-Host -ForegroundColor Cyan 'Resource Group Name is identified as-' $RGName
        $diagSa = [regex]::match($vm.DiagnosticsProfile.bootDiagnostics.storageUri, '^http[s]?://(.+?)\.').groups[1].value
        Write-Host -ForegroundColor Cyan 'Marking Disks for deletion...'
        $tags = @{"VMName"=$VMName; "Delete Ready"="Yes"}
        $osDiskName = $vm.StorageProfile.OSDisk.Name
        $datadisks = $vm.StorageProfile.DataDisks
        $ResourceID= (Get-Azdisk -Name $osDiskName).id
        New-AzTag -ResourceId $ResourceID -Tag $tags | Out-Null
        if ($vm.StorageProfile.DataDisks.Count -gt 0) {
            foreach ($datadisks in $vm.StorageProfile.DataDisks){
            $datadiskname=$datadisks.name 
            $ResourceID= (Get-Azdisk -Name $datadiskname).id 
            New-AzTag -ResourceId $ResourceID -Tag $tags | Out-Null
            }
        }
        if ($vm.Name.Length -gt 9) {
            $i = 9
        } else {
            $i = $vm.Name.Length - 1
        }
        $azResourceParams = @{
            'ResourceName' = $VMName
            'ResourceType' = 'Microsoft.Compute/virtualMachines'
            'ResourceGroupName' = $RGName
        }
        $vmResource = Get-AzResource @azResourceParams
        $vmId = $vmResource.Properties.VmId
        $diagContainerName = ('bootdiagnostics-{0}-{1}' -f $vm.Name.ToLower().Substring(0, $i), $vmId)
        $diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
        $saParams = @{
            'ResourceGroupName' = $diagSaRg
            'Name' = $diagSa
        }
        Write-Host -ForegroundColor Cyan 'Removing Boot Diagnostic disk..'
        Get-AzStorageAccount @saParams | Get-AzStorageContainer | where {$_.Name-eq $diagContainerName} | Remove-AzStorageContainer -Force
        Write-Host -ForegroundColor Cyan 'Removing Virtual Machine-' $VMName 'in Resource Group-' $RGName '...'
        $null = $vm | Remove-AzVM -Force
        Write-Host -ForegroundColor Cyan 'Removing Network Interface Cards, Public IP Address(s) used by the VM...'
        foreach($nicUri in $vm.NetworkProfile.NetworkInterfaces.Id) {
            $nic = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name $nicUri.Split('/')[-1]
            Remove-AzNetworkInterface -Name $nic.Name -ResourceGroupName $vm.ResourceGroupName -Force
        foreach($ipConfig in $nic.IpConfigurations) {
                if($ipConfig.PublicIpAddress -ne $null) {
                    Remove-AzPublicIpAddress -ResourceGroupName $vm.ResourceGroupName -Name $ipConfig.PublicIpAddress.Id.Split('/')[-1] -Force
                }
            }
        }
        Write-Host -ForegroundColor Cyan 'Removing OS disk and Data Disk(s) used by the VM..'
        Get-AzResource -tag $tags | where{$_.resourcegroupname -eq $RGName}| Remove-AzResource -force | Out-Null
        Write-Host -ForegroundColor Green 'Azure Virtual Machine-' $VMName 'and all the resources associated with the VM were removed sucesfully...'
        

        【讨论】:

        • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
        猜你喜欢
        • 1970-01-01
        • 2018-10-25
        • 1970-01-01
        • 2022-12-12
        • 2021-12-18
        • 1970-01-01
        • 1970-01-01
        • 2019-08-11
        • 1970-01-01
        相关资源
        最近更新 更多