[删除VM后从Azure资源管理器中删除VHD

问题描述 投票:3回答:3

提醒我,如果创建一个新的VM,然后删除它(及其关联的资源),则not实际上是删除相应的VHD文件-该文件保留在Blob存储中,从而阻止使用相同的主机名配置新计算机,并且浪费大量资金用于未使用和未使用的存储。我希望删除VHD和VM。

我在哪里可以找到关于如何处理此问题的current智慧的好文章?我所能找到的只是2013年以前的参考文献,这些参考文献显然针对“经典”版本。

我已经编写了以下代码来尝试纠正这种情况。我有两个用例,首先,我必须清除所有累积的垃圾,然后,我“只是”需要确保在删除以后的每台将来的机器之后进行清理。

write-output("Removing orphaned disks for hostname ""$hostname"" ...")
$storageContext = (Get-AzureRmStorageAccount | Where-Object{$_.StorageAccountName -match $azureStorage}).Context
$storageBlob = Get-AzureStorageBlob -Context $storageContext -Container "vhds"
$vhdList = $storageBlob | Where-Object{$_.Name -match "$hostname"}
foreach($disk in $vhdList) {
    $diskname = $disk.Name
    write-output("Removing VHD ""$diskname"" ...")
    Remove-AzureDisk -Diskname "$diskname" -DeleteVHD
    write-output("Removed VHD ""$diskname"" ... [OK]")
}
write-output("Removed orphaned disks ... [OK]")

现在,当我运行该命令时,我会得到很好的VHD文件清单,这些清单是我希望看到的(以及一些相应的“ * .status”文件)。但是,Remove-AzureDisk命令会产生错误Remove-AzureDisk : ResourceNotFound: The disk with the specified name does not exist,因此它实际上并不起作用。

[您会注意到,我在中途从新的“ ARM”命令切换到了“经典”版本-这可能是我的问题的一部分,但是我想出更好的命令并不走运。

更新:

这似乎可以解决问题:

# Verify that the OS VHD does not already exist
write-output("Checking for blocking VHD's ...")
$storageContext = (Get-AzureRmStorageAccount | Where-Object{$_.StorageAccountName -match $azureStorage}).Context
$storageBlob = Get-AzureStorageBlob -Context $storageContext -Container "vhds"
$vhdList = $storageBlob | Where-Object{$_.Name -match "$hostname"}
if ($vhdList) {
    write-output("There is an existing VHD blocking host ""$hostname"" in storage container ""$($storageContext.BlobEndPoint)vhds""):")
    foreach($vhdName in $vhdList.Name) {
        write-output("- $vhdName")
    }
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null
    $answer = [System.Windows.Forms.MessageBox]::Show("There is an existing VHD blocking host ""$hostname"" in storage container ""$($storageContext.BlobEndPoint)vhds"").`nDo you wish to remove the existing VHD file?" , "Create New VM" , $MB_YESNO)
    if ($answer -eq "YES" ) {
        # Remove VHD files
        foreach($diskName in $vhdList.Name) {
            write-output("- Removing VHD ""$diskName""")
            Remove-AzureStorageBlob -Blob $diskName -Container "vhds" -Context $storageContext
        }
        write-output("Checked for blocking VHD's ... [OK]")
    } else {
        exit(331)
    }
}

引起我注意的是,如果创建一个新的VM然后删除它(及其关联的资源),则实际上并没有删除相应的VHD文件-该文件保留在Blob存储中,...

powershell azure azure-powershell azure-resource-manager
3个回答
4
投票

Azure资源管理的一般智慧是,您不再考虑原子单位的资源,例如VM,存储,网络,而是开始将它们视为一组资源。一个资源组,;)


0
投票

检查服务器时钟和客户端时钟


© www.soinside.com 2019 - 2024. All rights reserved.