Match Windows Drive Letter to VM Disk with PowerCLI


Matching OS Drive Letters to VM Disks is definitely something each VMware admin has wanted at some point or another. Unfortunately, it is not the easiest of tasks. Using a suggestion I found in one of the forum posts that the solution might be a combination of two different scripts referenced here and here, I decided to try it out.

After combining and editing the scripts a bit it seems to work really well for me, although I am sure individual results may vary. Anyway here is the script…


$DiskInfo= @()
$Vm = "vmname"


$disks=Get-VM $vm| Get-HardDisk -DiskType "RawPhysical","RawVirtual"
$logtopart=Get-WmiObject -Class Win32_LogicalDiskToPartition -computername $VM 
$disktopart=Get-WmiObject Win32_DiskDriveToDiskPartition -computername $VM 
$logical=get-wmiobject win32_logicaldisk -computername $Vm
$volume=get-wmiobject win32_volume -computername $Vm
$partition=get-wmiobject win32_diskpartition -computername $Vm
if (($VmView = Get-View -ViewType VirtualMachine -Filter @{"Name" = $Vm})) {
    $WinDisks = Get-WmiObject -Class Win32_DiskDrive -ComputerName $VmView.Name
    foreach ($VirtualSCSIController in ($VMView.Config.Hardware.Device | where {$_.DeviceInfo.Label -match "SCSI Controller"})) {
        foreach ($VirtualDiskDevice in ($VMView.Config.Hardware.Device | where {$_.ControllerKey -eq $VirtualSCSIController.Key})) {
            $VirtualDisk = "" | Select SCSIController, DiskName, SCSI_Id, DiskFile,  DiskSize, WindowsDisk, NAA,drive, volumename
            $VirtualDisk.SCSIController = $VirtualSCSIController.DeviceInfo.Label
            $VirtualDisk.DiskName = $VirtualDiskDevice.DeviceInfo.Label
            $VirtualDisk.SCSI_Id = "$($VirtualSCSIController.BusNumber) : $($VirtualDiskDevice.UnitNumber)"
            $VirtualDisk.DiskFile = $VirtualDiskDevice.Backing.FileName
            $VirtualDisk.DiskSize = $VirtualDiskDevice.CapacityInKB * 1KB / 1GB
            $virtualdisk.naa=$disks|? {$ -like $VirtualDiskDevice.DeviceInfo.Label}|select -expand scsicanonicalname

            # Match disks based on SCSI ID
            $DiskMatch = $WinDisks | ?{($_.SCSIPort -2 ) -eq $VirtualSCSIController.BusNumber -and $_.SCSITargetID -eq $VirtualDiskDevice.UnitNumber}
            if ($DiskMatch){
                $VirtualDisk.WindowsDisk = "Disk $($DiskMatch.Index)"
            else {Write-Host "No matching Windows disk found for SCSI id $($VirtualDisk.SCSI_Id)"}

            $matchdisktopar=$disktopart|Where {$_.Antecedent -eq $diskmatch.__Path}
             $matchlogtopart=$logtopart| Where {$_.Antecedent -eq $matchdisktopar.Dependent}
             $logicalmatch=$logical| where {$_.path.path -eq $matchlogtopart.dependent}

             $DiskInfo += $VirtualDisk
    $DiskInfo | Out-GridView
else {Write-Host "VM $Vm Not Found"}

A couple things to note, you obviously need WMI open on the target windows machine, and the permissions to perform WMI calls against said machine.

Here is how it looks once completed.



The NAA is blank because none of these disks are RDMs, otherwise, NAA would be populated as well. Also for some reason it is not grabbing the C drive….might have to look at that.


Regardless it is pretty handy to have in the back pocket when you need it.


Let me know what you think

Using PowerCLI to Automate SRM Post Power On Commands

Long Title… But first things first. Ben Meadowcroft created several cmdlets and even a module using the SRM API. Having spent a good deal of time in the SRM API myself, let me tell you….IT IS A MESS. Long story short Ben, spent a lot of time creating this really helpful module, and you… Continue Reading

vNoob VCAP-DCD Experience

A bit delayed but here it is. I sat the DCD a while at VMworld San Fran. Having never taken an exam at VMworld, this was an entirely new experience for me. Well, the PearsonVue exam experience, was pretty much like any other, I just didn’t realize how much it would affect my VMworld experience. With… Continue Reading

Installing vSphere 6

Installing vSphere 6

Let’s get started installing vCenter. It is never a bad idea to know what screens and information you will need before you get get there! Already this looks SIGNIFICANTLY more streamlined than the vSphere 5.5 installations with SSO, Inventory Service, etc etc etc Next next next! I went with Embedded Deployment, this means everything necessary… Continue Reading

Interconnecting EMC VNX5200

Alright, for my first post back, I created a pretty diagram of the Unified connection scheme for the VNX5200. I found the documentation from EMC, although very good, never had a complete picture of everything once connected. Having to plan it out for my own implementation I decided to share it with you. This if… Continue Reading

Filed Under: EMC

And I’m Back!

Hello my three loyal readers. Sorry for the humongous delay in posting. Unfortunately it is really a combination of being really busy at work, and not having any time to play with new stuff. Fortunately I just got a new VNX5200 and a Dell Blade Chassis m1000e I have been installing/configuring/implementing. So all of you… Continue Reading

Finding Templates with PowerCLI

Although this doesn’t happen often sometimes I find that I have misplaced a template. I mean that a template I know exists is not in the inventory and I am not sure on which datastore I need to go to re-add it back to the inventory. Granted I don’t personally have many datastores to look… Continue Reading

Lower HIGH Java RAM Usage on vCenter Server

This is especially useful for SMB who don’t have large environments, and can’t throw resources at a vCenter Server This is what one of my vCenter Servers looked like recently To give some insight, this is a vCenter that manages around 3 hosts and 50 VMs. I quickly found a KB about java memory usage… Continue Reading

vNoob Learning Puppet

Yes, Puppet has been out for a while now. Yes, I am sure a ton of people already have “Getting Started with Puppet” posts. Well, I am going to do it. I have been wanting to become more familiar with Puppet for a while and just haven’t sat down to do it. Looking at the… Continue Reading