VMware Cloud Community
GeorgeSm
Contributor
Contributor

Match correctly windows disk to vmware harddisk

I've been searching all over for a way I could match disks inside windows to vmware's harddisk

The problem is when I have disks with same scsi id on more than one scsicontroller (for ex. (1:0)  and (0:0))

I found some scripts that compare the scsicontroller.bus number with the windows scsiport -1 but its not always the case..sometimes its -2 and sometimes its equal with no change. Any ideas? thanks in advance

28 Replies
Tamim02
Contributor
Contributor

  I’m still waiting for a reaction.

Reply
0 Kudos
pvpramodreddy
Contributor
Contributor

I have found a way for this and it worked for me so far.

Go to Disk Management > Disk X > Properties > General > Location

pastedImage_0.pngpastedImage_1.png

Here, Location 160 is SCSI Controller 0 and Target ID is the location inside the controller.

pastedImage_2.png

This means SCSI(0:0) is Location 160 (Target ID 0)

SCSI(0:1) is Location 160 (Target ID 0)

SCSI(0:15) is Location 160 (Target ID 15)

SCSI(1:5) is Location 192 (Target ID 5) and so on

tzuinam
Contributor
Contributor

I came across the same question as everyone reading this post and I found FMON script the most useful for my purpose: query vm hard disks using a windows drive letter. I reused his code to write a PS module that integrates with existing VMWare cmdlets to ease resizing VM disks.

I have attached the module. You can simply extract it to C:\Windows\System32\WindowsPowerShell\v1.0\Modules or any other path in $env:PSModulePath. It exports a cmdlet called Get-VMHardDiskFromDriveLetter

After installing the module you can use it just like this:

Get-VM <vmname> | Get-VMHardDiskFromDriveLetter -DriveLetter D

The output will be the VM hard disk where this 😧 logical disk exists.

Because the cmdlet needs to access the guest OS, you must provide -Credential parameter that you can feed with a PS Credential if the currently logged in user doesn't have admin access to the Guest OS.

Get-VM <vmname> | Get-VMHardDiskFromDriveLetter -DriveLetter D -Credential (Get-Credential)

Then you can use the result and pipe it to the Set-HardDisk cmdlet to resize the disk where the partition exists.

Get-VM <vmname> | Get-VMHardDiskFromDriveLetter -DriveLetter D -Credential (Get-Credential) | Set-HardDisk -SizeGB 10

Thanks  FMON.

Reply
0 Kudos
AlbertWT
Virtuoso
Virtuoso

Yes, until today 2018, there is no other way to consistently shows the result correctly.

/* Please feel free to provide any comments or input you may have. */
Reply
0 Kudos
philk33
Enthusiast
Enthusiast

How does one use this DiskPart script? Where does the output go?

Reply
0 Kudos
downstorffleon
Contributor
Contributor

saved my bacon on this one. i know im late to this party but ty

Reply
0 Kudos
wboaz
Enthusiast
Enthusiast

This is an old thread but I still couldn't find a good solution. But Powershell is more powerful now so I have come up with this.

###########
#Get-DiskFromLetter.ps1
# 2023-7-12 Wayne Boaz
# Script to determine vmware drive ID from Windows drive letter
###########
$viserver = read-host "Enter vcenter server where the host is located."
$computer = read-host "Enter Server Name."
$DriveLetter = read-host "Enter Drive Letter without the colon (i.e. F)."
Connect-VIServer $viserver
$diskid = get-partition -CimSession $computer -DriveLetter $DriveLetter | select -ExpandProperty diskid
$SerialNumber = get-disk -CimSession $computer | Where-Object {$_.path -eq "$diskid"} | select -ExpandProperty SerialNumber
get-vm $computer | Get-HardDisk | Where-Object {$_.ScsiCanonicalName -like "*$SerialNumber"} | select -Property Name, ScsiCanonicalName | ft -AutoSize
Disconnect-VIServer -server $viserver -confirm:$false

Reply
0 Kudos
rastickland
Enthusiast
Enthusiast

To take this thread to another level, if you have 4 scsi controllers and 4 disks on each controller on a vm.  lets say you are there is a vmdk that is currently identified in windows as disk 4 and vmdk on scsi controller 1:0:0.  for whatever reason, our end user states that no matter what windows disk 4  should always lineup with scsi controller 1:0:0.  So now its patch Tuesday and the vm restarts.  upon restarting this vm, the drive letters and windows disk are  in a completely different order after said restart than before the restart, particularly scsi controller 1:0:0 now shows as windows disk 8, with a different underlying windows disk controller(ie, 160 vs 254)

how does one ensure that disk1:0:0 is always(after a restart) windows disk 4 and stay on the same underlying windows disk information?

Reply
0 Kudos
jcollier5hole
Contributor
Contributor

Hi,

Thanks for the initial script it works great.  I updated it to use PS v7 and WinRM.  Help this helps someone.

 

$DiskInfo = @()
Connect-VIServer -Server <server name i.e. vcenter>
$computers = <list of computer name>
foreach ($computername in $computers) {
    $windiskdrives = Get-CimInstance  -computername $computername -Class Win32_DiskDrive -Property *
    $windiskpartitions = Get-CimInstance -ComputerName $computername -Class win32_diskpartition -Property *
    $vmdiskdrives = Get-harddisk -vm $computername
    $VMScsiController = Get-ScsiController -VM $computername
   
    foreach ($vmdiskdrive in $vmdiskdrives) {
        $VirtualDisk = "" | Select-Object SystemName, SCSIController, DiskName, SCSI_Id, DiskFile, VMDiskSize, WindowsDisk, DriveLetter, Description, WinDisksize, winSCSIID
        $VirtualDisk.systemname = $computername
        $VMUUID = ($vmdiskdrive.ExtensionData.Backing.uuid).replace("-", "")
        $SCSICont = $VMScsiController | where-object { $_.extensiondata.key -eq $vmdiskdrive.extensiondata.controllerkey }
        $VirtualDisk.SCSIController = $SCSICont.name
        $VirtualDisk.DiskName = $vmdiskdrive.name
        $VirtualDisk.SCSI_Id = "$($vmdiskdrive.ExtensionData.controllerkey - 1000) : $($vmdiskdrive.ExtensionData.unitnumber)"
        $VirtualDisk.DiskFile = $vmdiskdrive.ExtensionData.Backing.FileName
        $VirtualDisk.VMDiskSize = $vmdiskdrive.ExtensionData.CapacityinKB * 1KB / 1GB
        $diskmatch = $windiskdrives | where-object { $_.SerialNumber -eq $VMUUID }
        if ($DiskMatch) {
            $VirtualDisk.Winscsiid = "$($diskmatch.SCSIport - 2) : $($diskmatch.SCSITARGETID)"
            $VirtualDisk.WindowsDisk = "Disk $($diskmatch.Index)"
            $match1 = $windiskpartitions | where-object { $_.diskindex -eq $DiskMatch.index }
            ForEach ($partition in $match1) {
                $logicaldisk = $null
                $logicaldisk = Get-CimAssociatedInstance -InputObject $partition
                if ($logicaldisk.count -gt 1) {
                    $VirtualDisk.DriveLetter = $logicaldisk.DeviceID[2]
                    $VirtualDisk.Description = $logicaldisk.VolumeName[2]
                    $VirtualDisk.windisksize = $logicaldisk.Size[2] / 1GB
                }
            }
        }
    $DiskInfo += $VirtualDisk
    }
}
$DiskInfo | Out-GridView -OutputMode Multiple
Reply
0 Kudos