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?
- Create an empty $Results array
- Get all the VMs into $VMs.
- Look and see how many RDMs the VM has
- If zero, skip to the next one
- If it has RDMs, split the CRLF-separated text string into an array, $Devices
- Step through each item in the array, for each item:
- 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.
- 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.
- Add this object to the master $Results array
- Do some basic maths and display/update the progress bar
- Display the $Results array in a nice GUI with search feature by piping to Out-GridView