A high-performance, durable block storage designed to be used with Azure Virtual Machines and Azure VMware Solution.
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 :
- get the vms in deallocated state for more than x days ,
- if those VMs have a disk that is greater than 50GB size , tag the VMs and the disks
- 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
}
}
}
}
}
}