Matching VMDKs to Windows drive letters using PowerCLI

Recently one of the largest Zerto customers (10,000 protected VMs) reached out to me asking for help. It was early Friday evening, I already had a beer open, so I thought why not? Using PowerCLI they needed to match VMDKs to Windows drive letters in order to mark them as SWAP disks in their VM onboarding script. Pretty important as this saves bandwidth by Zerto not replicating changes to page files, TempDBs and SQL backup disks. In this post I’ll show you how to do this with no network access to the guest VM.

Which disk is which!

A quick google search turned up many examples of matching VMDKs to drive letters, but the majority relied on Get-WmiObject. This requires network access to the Guest which they don’t have, ruling out this option.

I next found a great example from Cody Hosterman here which doesn’t need network access to the guest VM, but it does rely on the advanced VM setting Disk.EnableUUID being enabled. Sod’s law they didn’t even have this setting present, never mind enabled, and adding it would require a reboot. Not a viable option for 10,000 VMs!

My solution was to combine the Disk.EnableUUID element of Cody’s module along with his use of ConvertTo-CSV to get usable data from PowerShell inside Invoke-VMScript (genius idea!). Iterating through each VMDK I match on disk UUID if present, if not then match based on the disk size in bytes. I also record which data point the disk was matched on with UUID being the preferred. Why? Because if you have 2 disks the same size and no disk UUID the script can only tell you all the possible drive letters. But this won’t happen to you, because you followed good practice by having each a different size, didn’t you?

The end result is this:

Here’s the result of running the script against the same VM but with disk UUID disabled:Below are 2 example scripts for you to download. One includes a prompt for credentials within the guest VM, the other will just pass on the credentials of the user running the script. The only major requirement other than PowerShell 5.1+ is the guest VM be on Server 2012 or above as it runs Get-Partition inside the VM. I’ve done my best to make sure both are easy to read/understand, and you can get them here: