Condividi tramite

使用 Azure PowerShell 查找并删除未附加的 Azure 托管和非托管磁盘

适用于:✔️ Linux VM ✔️ Windows VM ✔️ 灵活规模集 ✔️ 统一规模集

删除 Azure 中的虚拟机 (VM) 时,默认不删除附加到 VM 的任何磁盘。 此功能可帮助防止意外删除 VM 而导致的数据丢失。 删除虚拟机后,您将继续支付未附加磁盘的费用。 本文演示了如何查找并删除任何未附加的磁盘,以及如何减少不必要的成本。

注意

可以使用 Get-AzureDisk 命令获取 LastOwnershipUpdateTime 任何磁盘。 此属性表示磁盘状态上次更新的时间。 对于未附加的磁盘,此值显示磁盘未附加的时间。 对于新创建的磁盘,此属性为空,直到磁盘状态发生更改为止。

托管磁盘:查找并删除未附加的磁盘

以下脚本通过检查 ManagedBy 属性的值查找未附加的托管磁盘。 将托管磁盘附加到 VM 时, ManagedBy 属性包含 VM 的资源 ID。 未附加托管磁盘时,ManagedBy 属性为 null。 该脚本检查 Azure 订阅中的所有托管磁盘。 当脚本定位到一个 ManagedBy 属性设置为 null 的托管磁盘时,脚本会确定该磁盘未连接。

重要

首先,通过将 deleteUnattachedDisks 变量设置为 0 来运行脚本。 通过此操作可查找并查看所有未附加的托管磁盘。

在检查所有未附加磁盘后,再次运行脚本并将 deleteUnattachedDisks 变量设置为 1。 此操作将删除所有未附加的托管磁盘。

# Set deleteUnattachedDisks=1 if you want to delete unattached Managed Disks
# Set deleteUnattachedDisks=0 if you want to see the Id of the unattached Managed Disks
$deleteUnattachedDisks=0
$managedDisks = Get-AzDisk
foreach ($md in $managedDisks) {
    # ManagedBy property stores the Id of the VM to which Managed Disk is attached to
    # If ManagedBy property is $null then it means that the Managed Disk is not attached to a VM
    if($md.ManagedBy -eq $null){
        if($deleteUnattachedDisks -eq 1){
            Write-Host "Deleting unattached Managed Disk with Id: $($md.Id)"
            $md | Remove-AzDisk -Force
            Write-Host "Deleted unattached Managed Disk with Id: $($md.Id) "
        }else{
            $md.Id
        }
    }
 }

非托管磁盘:查找并删除未附加的磁盘

2026 年 3 月 31 日,非托管磁盘即将停用。 如果使用非托管磁盘,请在该日期之前迁移到托管磁盘。 有关停用的详细信息,请参阅 在 2026 年 3 月 31 日之前迁移 Azure 非托管磁盘

非托管磁盘是指以页 blob 形式存储在 Azure 存储帐户中的 VHD 文件。 以下脚本通过检查 LeaseStatus 属性的值,查找未附加的非托管磁盘(页 blob)。 如果非托管磁盘附加到 VM,则 LeaseStatus 属性设置为“已锁定”。 如果未附加非托管磁盘,则 LeaseStatus 属性设置为“未锁定”。 脚本会检查 Azure 订阅中所有 Azure 存储帐户中的所有非托管磁盘。 当脚本找到一个 LeaseStatus 属性设置为“未锁定”的非托管磁盘时,脚本判断该磁盘未附加。

重要

默认情况下,该脚本已将 deleteUnattachedVHDs 变量设置为 < 此设置允许您查找和查看所有未关联的非托管 VHD。

查看所有未附加的磁盘后,如果要删除所有这些 VHD,请将 deleteUnattachedVHDs 变量设置为 $true 并再次运行脚本。 此操作将删除所有未附加的 VHD。

# Set deleteUnattachedVHDs=$true if you want to delete unattached VHDs
# Set deleteUnattachedVHDs=$false if you want to see the Uri of the unattached VHDs
$deleteUnattachedVHDs = $false
$storageAccounts = Get-AzStorageAccount
foreach ($storageAccount in $storageAccounts) {
    $storageKey = (Get-AzStorageAccountKey -ResourceGroupName $storageAccount.ResourceGroupName -Name $storageAccount.StorageAccountName)[0].Value

    $context = New-AzStorageContext -StorageAccountName $storageAccount.StorageAccountName -StorageAccountKey $storageKey

    $containers = Get-AzStorageContainer -Context $context

    foreach ($container in $containers) {
        $blobs = Get-AzStorageBlob -Container $container.Name -Context $context
        #Fetch all the Page blobs with extension .vhd as only Page blobs can be attached as disk to Azure VMs
        $blobs | Where-Object {$_.BlobType -eq 'PageBlob' -and $_.Name.EndsWith('.vhd')} | ForEach-Object {
            if ($_.BlobProperties.LeaseStatus -eq 'Unlocked') {
                if ($deleteUnattachedVHDs) {
                    Write-Host "Deleting unattached VHD with Uri: $($_.ICloudBlob.Uri.AbsoluteUri)"
                    Remove-AzStorageBlob -Blob $_.Name -Container $container.Name -Context $context -Force
                    Write-Host "Deleted unattached VHD with Uri: $($_.ICloudBlob.Uri.AbsoluteUri)"
                } else {
                    Write-Host "Unattached VHD Uri: $($_.ICloudBlob.Uri.AbsoluteUri)"
                }
            }
        }
    }
}

后续步骤

有关详细信息,请参阅 删除存储帐户 并使用 PowerShell 标识孤立磁盘