Searching for RDM Disks using PowerCLI

If you have ever managed a VMware environment that contains virtual machines with RDM (Raw Device Mapping) disks then you will know it is a pain to track and manage RDMs, especially when trying to distinguish which VM has a specific RDM disk attached or if the RDM is still attached to a VM. You will also know that it is nearly impossible to find an RDM (and the corresponding VM) based on RDM’s LUN ID.

To ease the management burden of RDMs, I have written a script to search for a specific RDM by LUN ID or alternatively to get a list of all RDMs, their LUN ID, Capacity, Disk Identifier and the VM they are attached to. Below is the script and all of the details you need to get it up and running in your environment…

What are we trying to achieve?

First off, let’s look at an overview of what the “Searching for RDM Disks” script is trying to achieve. You can use the script in either of the following two ways:

Single Mode – In this mode, the script will find a specific RDM by its LUN ID. The results include the VM the RDM is attached to.

All Mode – Running the script in this mode will return a list of all the VMs that contain RDMs. In addition, it will return details about each of the RDM disks, including the LUN ID.

In both modes the results are outputted to the log file. For this reason, my PSLogging Module is a pre-requisite for this script to work successfully.

Script Overview

This script is built using my PowerCLI Script Template. If you are unfamiliar with it or would like to use it for yourself, you can check it out here – PowerCLI Script Template v2.

The script logging and the results are managed through my PSLogging module. If you want to use a standard PowerShell logging solution in all your scripts then you can download it here – PowerShell Logging v2.

When you run the script, you will be prompted for the the ESXi host or the vCenter server you want to connect to. If required, you will get prompted for credentials to connect to the environment you specified.

Next, the script will prompt you for the LUN ID that you want to search for. Alternatively, if you want to get the details of all the RDMs within you environment (i.e. running the script in All Mode) then enter the word “All” in the prompt.

The script will now enumerate every hard disk attached to every VM in the connected environment determining if each disk is an RDM or not. (Note: For this reason, the script might take a while to run depending on the size of your environment).

When an RDM is found it will either check that it matches the specified LUN ID or if running in All mode, the script will collect the relevant details of the RDM and then continue onto the next disk.

Finally the results are outputted to the log file specified in the Declarations section of the script.

The Script: Searching for RDM Disks using PowerCLI

How does it work?

For those of you who are interested in learning a bit more of how the script hangs together, I thought I would document some of the more intricate parts of the nuts and bolts below….

How To Find RDM disks using PowerCLI

Although it sucks, using PowerCLI the only way to find an RDM is to actually enumerate through all of the disks attached to a VM, checking for where Disk Type = “RawPhysical”.

If we knew what VM had the RDM then we could easily search that single VM’s disks, which would make the search really quick. In our situation however, we are either looking for all RDMs or we are searching for a specific RDM by LUN ID that could be attached to any of the VMs in the environment.

Therefore in this scenario, the only way to achieve this is to enumerate all the disks attached to all the VMs. This means we are essentially checking every single disk on every single VM within the environment. This is a SLOW process, but there is no other way. The good news is that when running the script in single mode, it will automatically stop once it has found the RDM, so that speeds things up considerably. It goes without saying that when running in all mode you have to traverse all disks of all VMs within the environment.

Having said all that, here is the actual command used to find RDM disks attached to a single VM (via the $VM variable) using PowerCLI:

Note: The $Disks variable is essentially an array that contains all of the disk objects that meet our search criteria.

Determining the LUN ID of an RDM disk

Once we got the list of RDMs within the environment we need to get each RDMs LUN ID and either output it to the log file (if running in All Mode) or check if it is the LUN ID we are searching for (if running in Single Mode).

Everything else is pretty much self-explanatory or just writing to the log file. If you are interested in finding out more information about how the logging works in the script then have a look at this article here – PowerShell Logging v2 – Easily create log files.

If you have any questions, suggestions for improvements or get stuck at any point let me know in the comments below.