PowerCli to get list of unused RDMs on VM Cluster – Updated

After finishing the last post and starting to use it after limited testing I found that my script wasn’t filtering out the Datastores as expected.  This information was presented to me via our storage team.  So I have went back to the Integrated Scripting Environment (ISE) to resolved my issues.

$Cluster = "Cluster name here"
#This Line gets all of the LUNs that are connected to the first host in the cluster.
$AllSCSILuns = (Get-Cluster $Cluster | Get-VMHost | Select -First 1 | Get-ScsiLun | Where {$_.LunType -eq "disk"}).CanonicalName
#This line gathers all of the Datastores Connected to the cluster.
$DataStores = (Get-Cluster $Cluster | Get-VMHost | Get-Datastore | where {$_.Type -eq "VMFS"} | Get-View).info.VMFS.Extent.diskname
#This does the first compare and removes the Datastores from the master list
$DSRemoved = (Compare-Object -ReferenceObject $AllSCSILUNs -DifferenceObject $DataStoreVolumes).InputObject
#This line gets all of the used RDM LUNs from the cluster
$UsedSCSILUNs = (Get-Cluster $Cluster | Get-VMHost | Get-VM | Get-HardDisk -DiskType "RawPhysical","RawVirtual").ScsiCanonicalName | Sort -Unique
#This does the final compare and removes the used RDM LUNs from the master list
(Compare-Object -ReferenceObject $DSRemoved -DifferenceObject $UsedSCSILUNs).InputObject

This is now how to properly get the unused RDM LUNs from your VM cluster.

– Stuart

2 thoughts on “PowerCli to get list of unused RDMs on VM Cluster – Updated”

  1. Great work! Thanks for sharing.
    having some errors using your script;

    Compare-Object : Cannot bind argument to parameter ‘DifferenceObject’ because it is null.
    At H:\VMware.PowerCLI\unused RDM LUNS.ps1:7 char:78
    + … erenceObject $AllSCSILUNs -DifferenceObject $DataStoreVolumes).InputO …
    + ~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Compare-Object], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CompareObjectCommand

    Compare-Object : Cannot bind argument to parameter ‘ReferenceObject’ because it is null.
    At H:\VMware.PowerCLI\unused RDM LUNS.ps1:11 char:34
    + (Compare-Object -ReferenceObject $DSRemoved -DifferenceObject $UsedSC …
    + ~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Compare-Object], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CompareObjectCommand

    1. David,
      Just from the output of the errors, it seems that the variables are not getting populated with the data to be able to compare them.

      You might need to run this script a line at a time to see if the variables are getting populated with the needed data.

      -Stuart

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.