Optimization of environmental costs. Disks. #01

The greater the environment, the more likely some resources are abandoned and unused. You know, someone requested a virtual machine, app service, but it turned out that it is unnecessary. Or some project or PoC finished and test resources are not removed. Due to this, it is important to look at your resources and check if can be removed or resized for example.

If you have virtual machines in your Azure environment, it’s always good to check some things related to disks. In this post, you can see how easy to check if some disks are not attached or if on some virtual machines disk space is unallocated.

Checking if some disks are orphaned you can do with the below Powershell command:

1
2
3
4
5
6
7
8
9
$existingDisks = Get-AzDisk

Write-Output "`nUnattached disks:"
foreach ($disk in $existingDisks) {
    If ($disk.DiskState.Contains('Unattached')) {
        $disk.Name
    }
}

Get-AzDisk gets all disks in the current subscription. Each disk has the property DiskState which can tell you if the disk is attached to some virtual machine or not. The above script checks if the DiskState property has value Unattached which means that it isn’t connected to any VM.

Another thing is that the disk can be connected to the virtual machine, but it isn’t initialized on the OS level. How to check that? It’s a more complex operation. You can’t do that from the Azure perspective, you have to check disk statuses directly on the OS. But relax, you don’t have to log into each virtual machine and check that manually. Let’s use the Run Command interface. There are a few ways to run custom scripts directly on virtual machine, each of them has its own pros and cons. Run Command will be the best option for us.

I prepared small script which will be run through Run Command interface on the virtual machine. Here it is:

1
2
3
4
5
6
7
$disksInfo = Get-Disk

foreach ($disk in $disksInfo) {
    If ($disk.PartitionStyle.Contains('RAW')) {
        Write-Output $true
    }
}

Important information is that the disk with the unallocated space has something called PartitionStyle with value RAW. The above script takes info about all physical disks in the machine and checks if any of those disks have PartitionStyle status equal RAW.

The process is: get info about all virtual machines in the subscription with the information if the virtual machine is running, run on each of them the custom script, aggregate info about the statuses of calling Run Command with the custom script, and show the list of virtual machines with unallocated spaces. The below scripts realize that process:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# List of vms
$vms = Get-AzVm -Status
$vmsToCheck = @()

# List of VMs with unallocated disks
foreach ($vm in $vms) {
    If ($vm.PowerState.Contains('VM running') -and $vm.StorageProfile.DataDisks.Length -gt 0) {
        $vmsToCheck += $vm
    }
}

foreach ($vm in $vmsToCheck) {
    $scriptBlock = {
        $runOutput = Invoke-AzVMRunCommand -ResourceId $args[0].Id -CommandId 'RunPowerShellScript' -ScriptPath '.\Test-IfVmHasUnallocatedDisks.ps1'
        If ($runOutput.Value[0].Message.Contains('True')) {
            return $args[0].Name
        }
    }

    $jobName = "$($vm.Name)-DisksCheck"

    $job = Start-Job -ScriptBlock $scriptBlock -ArgumentList $vm -Name $jobName
    $jobs += $job
}

Write-Host -NoNewline "`nChecking unallocated disks"
while ((Get-Job -Name "*-DisksCheck" | Where-Object State -eq Running)) {
    Write-Host -NoNewline "."
    Start-Sleep -Seconds 2
}

Write-Output "`nVMs with unallocated disks:"
Get-Job | Receive-Job

Small hack in the script above - calling Run Command takes quite a long time, so I’m running checking for chosen machines in parallel and after that, I’m waiting for when all jobs will be done.

Newsletter

Thank you for visiting my website. I hope you enjoyed content which I prepared and learned something valuable from it. If you want to be informed about my next entries or occasionally get a message with a collection of some interesting links, please subscribe to my newsletter. I will be extremely pleased if you do this and join my community!

  • By clicking button below you agree to send you news from my blog, about my products and services. Above data are stored in Mailchimp and I do not share them to anyone. More info you can find in privacy policy.

comments powered by Disqus