PowerCLI to Match VMDK to Windows Drive Letter

This has always been a thorn in my side. How to match a VMDK to a Windows drive. This was mostly an issue when our DBAs would put multi SQL instances on the same VMs that were configured in as a failover cluster. It would never fail that the the SQL instances would be configured with similar sized drives and need to be extended. So looking through the 5 disks all with the same size trying to determine which is the one that needs to be extended.

So after digging into this issue of finding the parts that match up, i was able to use the windows serial number for the drive, and the UUID in vCenter for the VMDK. So now it was just a simple game of make-a-match.

Serial numbers from windows
UUID gather from PowerCLI

So now that I have basically the Rosetta Stone for the mapping, I can easily make a table that shows which VMDK belongs to which Windows drive letter.

$DiskReport = @()
[regex]$Alpha = '[A-Z]'
$VM = "VMName"

$disks = Get-vm $VM | Get-harddisk
Foreach ($disk in $disks){
    $DriveLetter            = ""
    $UUID                   = $disk.extensiondata.backing.uuid
    If ($UUID){
        $SerialNumber       = $UUID.Replace('-','')
        $Script             = "(get-disk | Where Serialnumber -eq $SerialNumber | Get-Partition | Where DriveLetter -match $Alpha).DriveLetter"
        $DriveLetter        = (Invoke-VMScript -VM $VM -ScriptText $Script -ScriptType Powershell).ScriptOutput
        $DriveLetter        = $DriveLetter.Trim()
    $Line                   = "" | Select Name, CapacityGB, Parent, DriveLetter, ScsiCanonicalName, Filename, Controller, UnitNumber, UUID, DataStore
    $Line.Name              = $Disk.name
    $line.CapacityGB        = [math]::Round($Disk.CapacityGB,2)
    $line.Parent            = $Disk.Parent
    $line.DriveLetter       = $DriveLetter
    $line.ScsiCanonicalName = $Disk.ScsiCanonicalName
    $line.Filename          = $disk.Filename
    $line.Controller        = $disk.ExtensionData.ControllerKey -replace '100',''
    $line.UnitNumber        = $disk.ExtensionData.UnitNumber
    $line.UUID              = $UUID
    $line.DataStore         = ($disk.filename).split(" ")[0]
    $DiskReport            += $Line
$DiskReport | FT -AutoSize 
$DiskReport | Export-csv C:\scripts\logs\$VM.csv -NoTypeInformation
Omitted a few of the columns due to sensitive data. This output will also show the filename, datastore, and canonical name.

Now you can easily find the drive letter that corresponds to the VMDK.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.