天蓝色查找和删除未连接的磁盘

azure finding and removing unattached disks

我试图在 azure 上找到我的磁盘,但我似乎只有大约 20 个磁盘时才能找到一个磁盘。

语法:

Get-AzureDisk

遗憾的是,只有一个磁盘显示,我认为它来自经典 VM。

有人可以帮忙吗?

以防万一,查找未连接磁盘的语法

Get-AzureDisk | Where-Object {$_.AttachedTo -eq $Null}

语法对我来说是正确的。当您只是 运行 Get-AzureDisk 命令时,您会看到什么? AzureDisk 检索当前订阅 (https://msdn.microsoft.com/en-us/library/azure/dn495125.aspx) 的磁盘存储库中所有磁盘的数据。如果两个命令(您 运行 和 AzureDisks 的输出)的输出相同,则您可能选择了没有那么多未连接磁盘的订阅。

谢谢, 昂

您的 VM 是部署在 Classic 模型还是 ARM 模型中?

如果磁盘附加到经典 VM,并且您 select 订阅了包含这些磁盘的订阅,您应该能够通过 Get-AzureDisk 命令查看所有这些磁盘。 (您提供的语法正确。)

如果磁盘已附加到经典 VM,但您不确定自己 select 订阅了哪个订阅,请通过 Get-AzureSubscription 命令查看您当前的订阅。如果你想select其他订阅,使用Select-AzureSubscription命令。

如果您的磁盘已与 ARM VM 分离,或者您的 ARM VM 已被删除但未删除磁盘,您可以从 Azure 门户 (https://portal.azure.com) 中删除磁盘。 Select 存储帐户,然后单击未附加 VHD 所在的存储帐户,单击 Blob,单击包含 VHD 的容器,搜索 for/click vhd 并单击删除。

请注意,没有用于获取 Azure ARM VM 磁盘的直接 PowerShell 命令。但是,对于现有 VM 和附加磁盘,您可以使用 Get-AzureRmVM 将 return 数据磁盘作为 VM return 的一部分,然后使用 Remove-AzureRmVMDataDisk 命令删除ARM数据盘。 (这可能不适用于您的情况,因为您正在寻找未连接的磁盘。)

请确保您select正确的订阅并为不同的部署模型选择相应的command/action。

如果有帮助,请告诉我们。谢谢!

本文包含的脚本应该可以帮助您实现您的要求。

https://docs.microsoft.com/en-us/azure/virtual-machines/windows/find-unattached-disks

# Managed disks: Find and delete unattached disks
#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-AzureRmDisk

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-AzureRmDisk -Force

            Write-Host "Deleted unattached Managed Disk with Id: $($md.Id) "

        }else{

            $md.Id

        }

    }

 }

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

# Set deleteUnattachedVHDs=1 if you want to delete unattached VHDs
# Set deleteUnattachedVHDs=0 if you want to see the Uri of the unattached VHDs
$deleteUnattachedVHDs=0

$storageAccounts = Get-AzureRmStorageAccount

foreach($storageAccount in $storageAccounts){

    $storageKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $storageAccount.ResourceGroupName -Name $storageAccount.StorageAccountName)[0].Value

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

    $containers = Get-AzureStorageContainer -Context $context

    foreach($container in $containers){

        $blobs = Get-AzureStorageBlob -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 a Page blob is not attached as disk then LeaseStatus will be unlocked
            if($_.ICloudBlob.Properties.LeaseStatus -eq 'Unlocked'){

                  if($deleteUnattachedVHDs -eq 1){

                        Write-Host "Deleting unattached VHD with Uri: $($_.ICloudBlob.Uri.AbsoluteUri)"

                        $_ | Remove-AzureStorageBlob -Force

                        Write-Host "Deleted unattached VHD with Uri: $($_.ICloudBlob.Uri.AbsoluteUri)"
                  }
                  else{

                        $_.ICloudBlob.Uri.AbsoluteUri

                  }

            }

        }

    }

}

在我看来,这段代码干净、简单并且可以完成工作。

$disksList =  (Get-AzDisk | select  DiskState,Name,ResourceGroupName | where DiskState -eq 'Unattached')

  foreach($disk in $disksList){

    Remove-AzDisk -DiskName $disk.Name -ResourceGroupName $disk.ResourceGroupName -Force

  }