Automate Disk initialization on VM with Azure Desired State Configuration

A common scenario is that Azure VMs are being provisioned with some extra disks attached, those disks then needs to be initialized before use. Doing that for more than one server becomes time consuming so it needs to be automated.

I recently ran across this need again and recall solving it with DSC, of course i did not blog about it then so needed to redo part of the research. Correcting that now and “documenting” it.

Initialize disk is a simple task and here is the guide from Docs. https://docs.microsoft.com/en-us/azure/virtual-machines/windows/attach-managed-disk-portal#initialize-a-new-data-disk

But if you look to automate this there are several options with using script extension etc.

And you should always look to automate stuff you repeat.

Also to have the provisioning clean and standardized you need to deal with that Azure VMs sometimes have a DVD drive on the next available drive letter.

https://social.msdn.microsoft.com/Forums/azure/en-US/0adc77d0-2a86-4c7b-a232-bcb3b0b75602/sometimes-have-dvd-drive-in-azure-vmssometimes-not?forum=WAVirtualMachinesforWindows

I used DSC to move potential DVD drive and then initilize the disks and set drive letters.

Here is how you get started with DSC.

https://docs.microsoft.com/en-us/azure/automation/automation-dsc-getting-started

So follow above getting started guide and use below script when you come to the import configuration.

Here is the script that you need to compile:

Once compiled assign it to the node and DVD will be moved to Z and any disks will be initialized and assigned a drive letter.

VM disk details in the portal before DSC applied

VM disk details on the VM before DSC applied

VM disk details on the VM After DSC applied

DVD is moved and disks are setup, the script has a test segment that is defined to validate that no Raw disks are present, if true then reported as compliant.

The script uses only the module ‘PSDesiredStateConfiguration’ and no additional Xmodules, logic for that is to keep the configuration more managable and not needing to keep several modules updated and know specific syntaxes. Keeping it simple with SetScript and TestScript.

DSC is a powerful tool and should be leveraged more, maybe the future for DSC is Azure Functions or similar, true serverless maybe.

Hope this is useful and helpful, since i blogged about it now, i can find this solution when i face the need again

One thought on “Automate Disk initialization on VM with Azure Desired State Configuration

Leave a Comment