PowerCLI – Match Windows disk to VMware HardDisk

8

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 = "192.168.1.201"
$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 [email protected] 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!

Source: ARNIM VAN LIESHOUT

Share.

About Author

I am Adil Arif, working as a Senior Technical Support Engineer at Rubrik 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.

8 Comments

  1. Taylor Smith on

    This is a brilliant script, however on Windows Server 2016 VM’s specifically it will not report drives connected to the ‘0’/first SCSI controller.
    Example, on a file server with 4 SCSI controllers and 8 drives (2 on each controller), it will report drives 3 through 8, but for drives 1 and 2 it returns the error:
    No matching Windows disk found for SCSI id 0 : 0
    No matching Windows disk found for SCSI id 0 : 1
    Please help! I really need this to work. Any help is greatly appreciated!

  2. Hi,

    Thanks for the script

    Windows Disk details shows blank and getting this

    No matching Windows disk found for SCSI id 0 : 0
    No matching Windows disk found for SCSI id 0 : 1

    Please help.

  3. This helpful, and it appears to work, but there I’m not clear why a virtual SCSI controller Bus Number should win32_diskdrive SCSI Port “-2”?

  4. I am getting a bunch of errors but then the script actually generates an output window. However, it displays every disk 9 times; at least on the VM I tested.

    No matching Windows disk found for SCSI id 0 : 11
    The property ‘SCSIPort’ cannot be found on this object. Verify that the property exists and can be set.
    At W:\Vmware\Scripts\get_disks.ps1:24 char:29
    + $DiskMatch = $WinDisks | ?{($_.SCSIPort = 2) -eq $VirtualSCSIControll …
    + ~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

    • Just noticed that the output window doesn’t show anything in the WindowsDisk column. I guess it is not working then,

Leave A Reply