PowerCLI: Find a VM Based on RDM’s LUN ID

Let’s say you have a LUN ID that you clearly know is a RDM. How do you determine what virtual machine is associated with that disk?

Recently, I was prepping a cluster for routine ESXi patching. As part of my preparation, I scanned the cluster for RDM disks first to ensure that they were set to Perennially Reserved = True. After doing the RDM scan, I found a cluster that had a single RDM disk, but didn’t know which VM it was associated with. Although VMware has a Knowledge Base article (KB2001823) on how to do find RDM’s and which VM’s they’re associated with, it looks like it will find all RDM’s and VM’s across the entire vCenter Server. Since I know the cluster, and I know the RDM LUN ID, I wanted to narrow down the results for my particular needs.
Continue reading “PowerCLI: Find a VM Based on RDM’s LUN ID”


List RDM Disks in a Cluster Using PowerCLI

Hello everyone! Earlier today, I was looking for a way to list the RDM disks that are currently attached and in use by VMs within a specific cluster. See, we’re in the process of decommissioning an old SAN array, and I needed to verify that specific LUNs and RDMs were no longer in use. I came across this post over on the VMware Communities portal, but needed made a minor tweak, as I only wanted to search a specific cluster. By the way… HUGE thanks to user LucD for posting the original script! After running this script, I was then able to search the output to ensure that the LUN WWNs I needed to decommission weren’t in the list.

$report = @()
$vms = Get-Cluster -name "Cluster01" | Get-VM | Get-View
foreach($vm in $vms){
     foreach($dev in $vm.Config.Hardware.Device){
          if(($dev.gettype()).Name -eq "VirtualDisk"){
               if(($dev.Backing.CompatibilityMode -eq "physicalMode") -or 
               ($dev.Backing.CompatibilityMode -eq "virtualMode")){
                    $row = "" | select VMName, VMHost, HDDeviceName, HDFileName, HDMode, HDsize, HDDisplayName
                    $row.VMName = $vm.Name
                    $esx = Get-View $vm.Runtime.Host
                    $row.VMHost = ($esx).Name
                    $row.HDDeviceName = $dev.Backing.DeviceName
                    $row.HDFileName = $dev.Backing.FileName
                    $row.HDMode = $dev.Backing.CompatibilityMode
                    $row.HDSize = $dev.CapacityInKB
                    $row.HDDisplayName = ($esx.Config.StorageDevice.ScsiLun | where {$_.Uuid -eq $dev.Backing.LunUuid}).DisplayName
                    $report += $row