Find mapping between VMWare virtual disk and Harddisk inside Windows VM
From Notes_Wiki
Home > VMWare platform > VMWare vSphere or ESXi > Find mapping between VMWare virtual disk and Harddisk inside Windows VM
If a VM has two hard-disks of same capacity (eg 500GB) then it is difficult to associate between VM virtual disk and hard-drive inside VM (C:, D:) as there is no way to co-relate between virtual disk provided by VMWare and disk visible inside Windows. Older version of Windows could perhaps do this based Location - See https://kb.vmware.com/s/article/2021947 But at least in Windows 10 and later versions this is not possible.
Not working steps are mentioned below:
- For one time installation of VMWare modules and enabling self-signed certificate, start powershell with administrator privileges and execute
- [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
- Install-Module VMware.PowerCLI
- Set-ExecutionPolicy RemoteSigned
- Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
- Connect to vCenter server Required each time a new powershell is started'
- Connect-VIServer #And then give vcenter ip followed by empty line. Them on popup enter username and password for vcenter.
- #OR
- Connect-VIServer -Server <vCenter-ip> -User administrator@vsphere.local -Password <Administrator-password>
- Replace the below variables with appropriate vm-name (vcenter level) and computer name (Windows level):
- #Variables
- $Vm = Get-VM -Name 'saurabh-vmware-powercli-test'
- $ComputerName = 'Testpc1'
- Then copy paste the lines into the powershell where vcenter login was completed successfully.
- Copy / paste rest of this script:
$obj_DiskDrive = @() $obj_LogicalDisk = @() $obj_LogicalDiskToPartition = @() $obj_DiskDriveToDiskPartition = @() $obj_VMView = @() $obj_DiskInfos = @() #Get wmi objects $obj_DiskDrive = Get-WmiObject -Class win32_DiskDrive -ComputerName $ComputerName $obj_LogicalDisk = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $ComputerName $obj_LogicalDiskToPartition = Get-WmiObject -Class Win32_LogicalDiskToPartition -ComputerName $ComputerName $obj_DiskDriveToDiskPartition = Get-WmiObject -Class Win32_DiskDriveToDiskPartition -ComputerName $ComputerName #Get vm $obj_VMView = Get-View -ViewType VirtualMachine -Filter @{"Name" = "$($Vm.Name)"} #Get vm disk $obj_VMDisk = Get-HardDisk -VM $Vm #Match the informations foreach ($obj_vmWareSCSIController in ($obj_VMView.Config.Hardware.Device | Where-Object -FilterScript {$_.DeviceInfo.Label -match "SCSI"})) { foreach ($obj_vmWareDiskDevice in ($obj_VMView.Config.Hardware.Device | Where-Object -FilterScript {$_.ControllerKey -eq $obj_vmWareSCSIController.Key})) { $obj_tempDiskInfos = "" | Select-Object -Property Date, vCenterName, vmName, vmWareSCSIController, wmWareSCSIID, vmWareDiskName, vmWareDiskFile, vmWareSizeGB, WindowsSerialNumber, WindowsSCSIBus, WindowsSCSILogicalUnit, WindowsSCSIPort, WindowsSCSITargetId, WindowsDisk, WindowsDriveLetter, WindowsLocicalDiskSizeGB, WindowsLocicalDiskFreeSpaceGB, WindowsLocicalDiskUsedSpaceGB #Select WMI object $obj_currentDiskDrive = @() $obj_currentDiskDrive = $obj_DiskDrive | Where-Object -FilterScript {$_.SerialNumber -eq $obj_vmWareDiskDevice.Backing.Uuid.Replace("-","")} $obj_currentDiskDriveToDiskPartition = @() $obj_currentDiskDriveToDiskPartition = $obj_DiskDriveToDiskPartition | Where-Object -FilterScript {$_.Antecedent -eq $obj_currentDiskDrive.Path} $obj_currentLogicalDiskToPartition = @() $obj_currentLogicalDiskToPartition = $obj_LogicalDiskToPartition | Where-Object -FilterScript {$_.Antecedent -eq $obj_currentDiskDriveToDiskPartition.Dependent} $obj_currentLogicalDisk = @() $obj_currentLogicalDisk = $obj_LogicalDisk | Where-Object -FilterScript {$_.Path.Path -eq $obj_currentLogicalDiskToPartition.Dependent} #Select vmWare object $obj_CurrentvmWareHarddisk = @() $obj_CurrentvmWareHarddisk = $obj_VMDisk | Where-Object -FilterScript {$_.Name -eq $obj_vmWareDiskDevice.DeviceInfo.Label} #Generate output $obj_tempDiskInfos.Date = Get-Date -Format "yyyy.MM.dd HH:mm:ss" $obj_tempDiskInfos.vCenterName = $defaultVIServer.Name $obj_tempDiskInfos.vmName = $Vm.Name $obj_tempDiskInfos.vmWareSCSIController = $obj_vmWareSCSIController.DeviceInfo.Label $obj_tempDiskInfos.wmWareSCSIID = "$($obj_vmWareSCSIController.BusNumber) : $($obj_vmWareDiskDevice.UnitNumber)" $obj_tempDiskInfos.vmWareDiskName = $obj_vmWareDiskDevice.DeviceInfo.Label $obj_tempDiskInfos.vmWareDiskFile = $obj_vmWareDiskDevice.Backing.FileName $obj_tempDiskInfos.vmWareSizeGB = $obj_CurrentvmWareHarddisk.CapacityGB $obj_tempDiskInfos.WindowsSerialNumber = $obj_currentDiskDrive.SerialNumber $obj_tempDiskInfos.WindowsSCSIBus = $obj_currentDiskDrive.SCSIBus $obj_tempDiskInfos.WindowsSCSILogicalUnit = $obj_currentDiskDrive.SCSILogicalUnit $obj_tempDiskInfos.WindowsSCSIPort = $obj_currentDiskDrive.SCSIPort $obj_tempDiskInfos.WindowsSCSITargetId = $obj_currentDiskDrive.SCSITargetId $obj_tempDiskInfos.WindowsDisk = $obj_currentDiskDrive.Path.Path $obj_tempDiskInfos.WindowsDriveLetter = ($obj_currentLogicalDisk).Caption $obj_tempDiskInfos.WindowsLocicalDiskSizeGB = $obj_currentLogicalDisk.Size / 1GB $obj_tempDiskInfos.WindowsLocicalDiskFreeSpaceGB = $obj_currentLogicalDisk.FreeSpace / 1GB $obj_tempDiskInfos.WindowsLocicalDiskUsedSpaceGB = ($obj_currentLogicalDisk.Size / 1GB) - ($obj_currentLogicalDisk.FreeSpace / 1GB) $obj_DiskInfos += $obj_tempDiskInfos } } $obj_DiskInfos
Refer:
- Powercli to work with self signed certificates - https://www.techcrumble.net/2019/03/power-cli-error-invalid-server-certificate-use-set-powercliconfiguration-to-set-the-value-for-the-invalidcertificateaction-option/
- Accepting remotesigned VMWare powercli modules - https://www.partitionwizard.com/clone-disk/running-scripts-is-disabled-on-this-system.html
- Solving nugen issue by changing security protocol - https://www.msdigest.net/2021/06/unable-to-install-nuget-provider-for-powershell-on-windows-server-2016/
- Stack overflow article with above powerscript - https://stackoverflow.com/questions/26232832/powershell-match-virtual-hard-disks-in-virtual-center-with-their-disk-labels
Home > VMWare platform > VMWare vSphere or ESXi > Find mapping between VMWare virtual disk and Harddisk inside Windows VM