Quantcast
Channel: DIY IT Shop » powershell
Viewing all articles
Browse latest Browse all 9

VMWare: Map Logical Drives to VMDKs Using PowerCLI (Windows)

$
0
0

Ever had the need to check which logical drives corresponded to which virtual disk attached to a virtual machine? I have!

Ever needed to do that without spending more than 5 minutes comparing the settings? Me too!

This PowerShell function will help you accomplish just that.

This function will produce a list mapping each VMDK attached to a VMWare virtual machine to a logical drive in windows. We accomplish this by comparing the SCSI ID’s in the VM Settings with the SCSI Bus ID’s in Windows.

Naturally, there are a couple caveats since we have to use underlying interfaces for this functionality:

  1. You must have PowerCLI, the PowerShell extensions from VMWare,  installed. You can download that here.
  2. You must have a connection established to your VirtualCenter host with credentials that allows you to do these things NEMO!
  3. You must be able to reach the WMI of the Guest OS of the machine from the computer you are running this on. That means possible firewall / routing issues.
  4. You must have administrative credentials for that Guest OS. Preferably, you are running PowerCLI with those credentials, otherwise, this will take some modification.

Save the following to a *.ps1 file in your favorite import location, add it to your profile or dot source it in, then have at it! Pipe it, filter it, select it, format it, and go for it!

 

Function Get-VMDiskMap {
	[Cmdletbinding()]
	param([Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)][string]$VM)
	begin {
	}
	process {
		if ($VM) {
			$VmView = Get-View -ViewType VirtualMachine -Filter @{"Name" = $VM}	   
			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 VM,SCSIController, DiskName, SCSI_Id, DiskFile,  DiskSize, WindowsDisks
					$VirtualDisk.VM = $VM
					$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

					$LogicalDisks = @()
					# Look up path for this disk using WMI.
					$thisVirtualDisk = get-wmiobject -class "Win32_DiskDrive" -namespace "root\CIMV2" -computername $VM | where {$_.SCSIBus -eq $VirtualSCSIController.BusNumber -and $_.SCSITargetID -eq $VirtualDiskDevice.UnitNumber}
					# Look up partition using WMI.
					$Disk2Part = Get-WmiObject Win32_DiskDriveToDiskPartition -computername $VM | Where {$_.Antecedent -eq $thisVirtualDisk.__Path}
					foreach ($thisPartition in $Disk2Part) {
						#Look up logical drives for that partition using WMI.
						$Part2Log = Get-WmiObject -Class Win32_LogicalDiskToPartition -computername $VM | Where {$_.Antecedent -eq $thisPartition.Dependent}
						foreach ($thisLogical in $Part2Log) {
							if ($thisLogical.Dependent -match "[A-Z]:") {
								$LogicalDisks += $matches[0]
							}
						}
					}

					$VirtualDisk.WindowsDisks = $LogicalDisks
					Write-Output $VirtualDisk
				}
			}
		}
	}
	end {
	}
}

 


Viewing all articles
Browse latest Browse all 9

Trending Articles