天蓝色找到并删除未连接的磁盘

时间:2016-08-12 13:52:59

标签: powershell azure azure-storage azure-storage-blobs

我试图在azure上找到我的磁盘,但是当我至少有20个左右时,我似乎只得到一个磁盘。

语法:

Get-AzureDisk

可悲的是,只有一个磁盘显示我相信来自classicVM。

有人可以帮忙吗?

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

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

4 个答案:

答案 0 :(得分:1)

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

谢谢, 昂

答案 1 :(得分:1)

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

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

如果磁盘已连接到经典虚拟机,但您不确定选择了哪个订阅,请通过 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数据磁盘。 (这可能不适用于您的方案,因为您正在寻找未连接的磁盘。)

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

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

答案 2 :(得分:0)

本文包含的脚本应可帮助您满足要求。

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

                  }

            }

        }

    }

}

答案 3 :(得分:0)

在我看来,这段代码干净、简单,并且可以胜任。

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

  foreach($disk in $disksList){

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

  }