【问题标题】:azure finding and removing unattached disks天蓝色查找和删除未连接的磁盘
【发布时间】:2016-12-19 13:42:24
【问题描述】:

我正在尝试在 azure 上找到我的磁盘,但我似乎只有一个磁盘至少有 20 个左右。

语法:

Get-AzureDisk

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

有人可以帮忙吗?

以防万一,查找未附加磁盘的语法

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

【问题讨论】:

    标签: powershell azure azure-storage azure-blob-storage


    【解决方案1】:

    您的虚拟机是部署在经典模型还是 ARM 模型中?

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

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

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

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

    请确保您选择了正确的订阅并为不同的部署模型选择相应的命令/操作。

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

    【讨论】:

    • 感谢您的回复。我只是在寻找 powershell 命令,但希望你们将来会添加一个。
    • 感谢您的反馈。该团队正在研究类似于 Get-AzureDisk 的东西。请跟上 Azure PowerShell 未来版本的步伐。谢谢!
    【解决方案2】:

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

    谢谢, 昂

    【讨论】:

    • 嗨,我正在尝试使用基于 Azure Web 的 CLI 在我的资源组中查找此类未附加的磁盘,但在响应中没有看到“diskState”参数:我在 Azure Web CLI控制台并执行命令“az disk list --resource-group XXXXXX --query '[].{name:name,diskState:diskState}'”
    【解决方案3】:

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

    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
    
                      }
    
                }
    
            }
    
        }
    
    }
    

    【讨论】:

      【解决方案4】:

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

      $disksList =  (Get-AzDisk | select  DiskState,Name,ResourceGroupName | where DiskState -eq 'Unattached')
      
        foreach($disk in $disksList){
      
          Remove-AzDisk -DiskName $disk.Name -ResourceGroupName $disk.ResourceGroupName -Force
      
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-30
        • 2016-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-05
        • 1970-01-01
        相关资源
        最近更新 更多