PowerCLI – Match Windows disk to VMware HardDisk


Hello Folks,

I was recently working with one of the customers and the task was to match Windows disk to VMware HardDisk.

Well, there are various ways that you could do this, but I was looking at a script which would do this task and not to perform this manually.

So I turned to Google to check for the information but was overwhelmed with various information I got and finally found a nice little script which does the job.

Note: I do not take any credit for the script, I am just sharing this on the blog for easy consumption.

The reason we were doing this was because there were multiple disks with the same size and there was no documentation which pointed which VMDK was for which Windows disk.

With that being said, here is the script.

# Initialize variables
# $VCServerList is a comma-separated list of vCenter servers
$VCServerList = “”
$DiskInfo= @()
$Vm = “vCenter55-A”
$Cred = get-credential Test.local\administrator

# Set Default Server Mode to Multiple
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false | Out-Null
# Connect to vCenter Server(s)
foreach ($VCServer in $VCServerList) {Connect-VIServer -Server “$VCServer” | Out-Null}

if (($VmView = Get-View -ViewType VirtualMachine -Filter @{“Name” = $Vm})) {
$WinDisks = Get-WmiObject -Class Win32_DiskDrive -Credential $Cred -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
$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
# 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)”}
$DiskInfo += $VirtualDisk
$DiskInfo | Out-GridView
else {Write-Host “VM $Vm Not Found”}

Disconnect-VIServer * -Confirm:$false

The script is pretty self-explanatory.

You would have to provide the information about the vCenter Server. It accepts more than one vCenter Server separated by a comma.

You would then have to provide the name of the Virtual Machine for which you have to match Windows Disk to VMware HardDisk.

Well, I wanted to let you know at first I wasn’t getting the desired output. When I was using WMI to query the Disk information, I was getting an Access Denied error as below.

PowerCLI - Match Windows Disk to VMware HardDisk

The reason behind this error message was that I was providing the information about the account used to query the Windows machine.

I then added an extra line which will ask for the user account used to query the Windows machine. This account needs to have sufficient rights.

So, when you execute the script on PowerCLI, there will be two prompts. The first prompt will be for the AD account used to query the WMI information on the Windows machine.

The second prompt is for connecting to the vCenter Server. In my case, I was using administrator@vsphere.local account. You can use any account that has sufficient rights.

Once the script runs successfully, you should see an output similar to below.

PowerCLI - Match Windows Disk to VMware HardDisk

Well, that is all we have for today. I hope this has been informative and thank you for reading!



About Author

I am Adil Arif, working as a Technical Support Engineer at VMware as well as an independent blogger and founder of Enterprise Daddy. In my current role, I am supporting infrastructure related to Windows and VMware datacenters.

1 Comment

Leave A Reply

If so, please join the community.  I’d love to send you exclusive content right to your email, and I’ll send you a free copy of my book, VMware vSphere 6.0 Lab on Workstation too!
Enter your name and email below, and I’ll see you on the inside!