How to find Disks that are not attached/used more than xx days?

Yahav Horev | CloudTeam.ai 136 Reputation points
2021-06-06T15:44:15.187+00:00

Hey guys i need your help please.

i need to find a way how to locate all my disks in my azure account that are not in used or not attached more than xx days ,
please help me to find the best way to do that ..

Azure Disk Storage
Azure Disk Storage

A high-performance, durable block storage designed to be used with Azure Virtual Machines and Azure VMware Solution.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Yahav Horev | CloudTeam.ai 136 Reputation points
    2021-06-13T11:55:12.273+00:00

    Hey !
    First of all i am so gradeful for your Detalied answers!!
    i have been out for a few days and my task was changed to :

    1. get the vms in deallocated state for more than x days ,
    2. if those VMs have a disk that is greater than 50GB size , tag the VMs and the disks
    3. delete the tagged resources.

    but anyway you answer to find those disks will help me now.
    i will give it a try and will let you know if it worked.
    ** sharing with you my code for this one :)

    $ids = New-Object System.Collections.ArrayList
    $mergedTags = @{"Candidate"="Delete Me"}
    $vms =  (Get-AzVM -Status)
        foreach ($vm in $vms ) {
           $getStatuses = Get-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Status  
           $getTodaysDate = Get-Date
           $ts = New-TimeSpan  -Start (($getStatuses.Statuses).Time).DateTime -End $getTodaysDate  
               [bool]$flag = 0
               if (($vm.PowerState -eq "VM deallocated") -AND ($ts.Days -gt 30)) {
               $ids.Add($vm.Id)
                    foreach ($disk in $getStatuses.Disks.Name) {
                        $diskSize = Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -Name $disk
                        if ($diskSize.DiskSizeGB -gt 50) {
                            $flag = 1
                            $ids.Add($diskSize.Id)
                            if ($flag = 1) {
                                Update-AzTag -ResourceId $vm.Id -Tag $mergedTags -Operation Merge
                                Update-AzTag -ResourceId $diskSize.Id -Tag $mergedTags -Operation Merge
                            }
                        }
                    }
                }
            }
        } 
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.