Search for VMware RDMs

A storage admin recently asked me if a couple of LUNs were still in use. They had very little info about them, but knew they were presented to the vSphere hosts. So I wrote a quick script to find all RDMs (Raw Device Mapped LUNs) attached to virtual machines, and list them.

I’m using my new favourite PowerShell display format, namely Out-GridView as it has a very handy filter box at the top. So you can simply type in the NAA of the LUN or the server name and the result are filtered on the fly.

It has to query each VM to see if it has any RDMs and then get their info, so can take a while to run. I’ve added a progress bar to give you some idea as to how it is progressing. You obviously need the PowerCLI modules loaded, and need to have already run Connect-VIServer before running this script.

$Result = @()
$VMs = Get-VM
$VMsProcessed = 0
foreach($VM in $VMs){
    if($VM.HardDisks.DeviceName.Count -gt 0){
        $Devices = $VM.HardDisks.DeviceName -split "`r"
        foreach($Device in $Devices){
            $DeviceNAA = $Device.Substring(14,32)
            $obj = New-Object system.object
            $obj | Add-Member -MemberType NoteProperty -Name VMName -Value $VM.Name
            $obj | Add-Member -MemberType NoteProperty -Name PowerState -Value $VM.PowerState
            $obj | Add-Member -MemberType NoteProperty -Name DeviceNAA -Value $DeviceNAA
            $Result += $obj
        }
    }
    $VMsProcessed++
    Write-Progress -Activity "Getting VM details" -PercentComplete ($VMsProcessed/$VMs.Count*100)
}
$Result | Out-GridView

How does it work?

  1. Create an empty $Results array
  2. Get all the VMs into $VMs.
  3. Look and see how many RDMs the VM has
    1. If zero, skip to the next one
    2. If it has RDMs, split the CRLF-separated text string into an array, $Devices
    3. Step through each item in the array, for each item:
      1. Extract the NAA from the text string (which is the full vml.<etc.> form that you see in the vSphere client, put the extracted NAA text into $DeviceNAA.
      2. Create a new temporary object, $obj, add to it the VM Name, Power State (you might want to know if the VM connected to the RDM is actually in use or not), and the NAA text string.
      3. Add this object to the master $Results array
  4. Do some basic maths and display/update the progress bar
  5. Display the $Results array in a nice GUI with search feature by piping to Out-GridView
This entry was posted in PowerShell, Scripting, vSphere and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s