Find LUNs Presented to Multiple Clusters

After some cluster migrations it appears that not all of the LUN presentations got cleaned up. I needed to find the LUNs that were shared by different clusters so I could determine which one they should belong to and remove access for the other cluster(s). PowerCLI to the rescue.

param (
    [VMware.VimAutomation.ViCore.Impl.V1.Inventory.ClusterImpl[]]$clusters = $(throw "A list of clusters must be specified."),
    [switch]$all
)
 
# A hash table to keep track of what we have seen.
$lunMap = @{}
 
# Get all of the LUNs and the clusters they are on
foreach ($cluster in $clusters) {
    Write-Host "." -NoNewline
    foreach ($vmhost in Get-VMHost -Location $cluster) {
        foreach ($lun in (Get-ScsiLun -VmHost $vmhost)) {
            $id = $lun.ExtensionData.Uuid
            if ($lunMap.containsKey($id)) {
                if (! ($lunMap[$id].Clusters -contains $cluster.Name)) {
                    # Update the cluster list
                    $lunMap[$id].Clusters += $cluster.Name
                }
            }
            else {
                # Create an entry for this LUN
                $info = "" | Select-Object CanonicalName, Clusters
                $info.Clusters = @($cluster.Name)
                $info.CanonicalName = $lun.CanonicalName
                $lunMap[$id] = $info
            }
        }
    }
}
 
# Output the ones we care about
foreach ($key in $lunMap.Keys) {
    if (($lunMap[$key].Clusters.length -gt 1) -or $all) {
        "" | Select-Object @{Name="ScsiLunUuid"; Expression={$key}}, @{Name="CanonicalName"; Expression={$lunMap[$key].CanonicalName}},
                            @{Name="Clusters"; Expression={$lunMap[$key].Clusters}}
    }
}
<#
.SYNOPSIS
Find the LUNs that are shared by clusters.
 
.PARAMETER clusters
An array of the clusters you want to check.  The script is generally only
useful with 2 or more clusters.
 
.PARAMETER all
Output all of the LUN -> cluster details instead of just the duplicates
 
.EXAMPLE
.\Get-DuplicatePresentation (Get-Cluster)
 
.EXAMPLE
.\Get-DuplicatePresentation (Get-Cluster cluster1,cluster2)
#>

Leave a Comment

Your email address will not be published.