param( [string]$ClusterName ) $csvName = ".\$($ClusterName)-$(Get-Date -Format 'yyyy-MM-dd-hh-mm')-LUN.csv" $rndNum = Get-Random -Maximum 99999 $LunInfoDef = @' public string ClusterName; public string CanonicalName; public string UsedBy; public string DSC; public string SizeGB; '@ $LunInfoDef = "public struct LunInfo" + $rndNum + "{`n" + $LunInfoDef $esxServers = Get-Cluster $ClusterName | Get-VMHost | Sort-Object -Property Name $esxServers | ForEach-Object { $LunInfoDef += ("`n`tpublic string " + ($_.Name.Split(".")[0]) + ";") } $LunInfoDef += "`n}" Add-Type -Language CsharpVersion3 -TypeDefinition $LunInfoDef $scsiTab = @{} $esxServers | ForEach-Object { $esxImpl = $_ # $scsiTab layout # Key: # # Value: # 1) canonicalname # 2) datastorename or VM/harddiskname # 3) capacity (GB) # 4) LunId # 5) datastoreclustername # # Get SCSI LUNs if (([Version]$esxImpl.Version).Major -lt 6) { $esxImpl | Get-ScsiLun | Where-Object { $_.LunType -eq "Disk" } | ForEach-Object { $key = $esxImpl.Name.Split(".")[0] + "#" + $_.CanonicalName.Split(".")[1] if (!$scsiTab.ContainsKey($key)) { $lunId = $_.RuntimeName.Split('L')[1] $scsiTab[$key] = $_.CanonicalName, "", $_.CapacityGB, $lunId, "" } } } Else { $esxImpl.ExtensionData.Config.StorageDevice.ScsiLun | Where-Object { $_.LunType -eq 'disk' } | ForEach-Object { $key = $esxImpl.Name.Split(".")[0] + "#" + $_.CanonicalName.Split(".")[1] $capacityGB = $_.Capacity.Block * $_.Capacity.BlockSize / 1GB $lunUuid = $_.Uuid $lun = $esxImpl.ExtensionData.Config.StorageDevice.MultipathInfo.Lun | Where-Object { $_.Id -eq $lunUuid } $lunId = $lun.Path[0].Name.Split('L')[1] $scsiTab[$key] = $_.CanonicalName, "", $capacityGB, $lunId, "" } } # Get the VMFS datastores $esxImpl | Get-Datastore | Where-Object { $_.Type -eq "VMFS" } | Get-View | ForEach-Object { $dsName = $_.Name $dscName = '' if ($_.Parent.Type -eq 'StoragePod') { $dscName = Get-View -Id $_.Parent -Property Name -Server $esxImpl.Uid.Split('@')[1].Split(':')[0] | Select-Object -ExpandProperty Name } $_.Info.Vmfs.Extent | ForEach-Object { $key = $esxImpl.Name.Split(".")[0] + "#" + $_.DiskName.Split(".")[1] $scsiTab[$key][1] = $dsName $scsiTab[$key][4] = $dscName } } } # Get the RDM disks Get-Cluster $ClusterName | Get-VM | Get-View | ForEach-Object { $vm = $_ $vm.Config.Hardware.Device | Where-Object { $_.gettype().Name -eq "VirtualDisk" } | ForEach-Object { if ($_.Backing.PSObject.Properties['CompatibilityMode'] -and "physicalMode", "virtualmode" -contains $_.Backing.CompatibilityMode) { $disk = $_.Backing.LunUuid.Substring(10, 32) $key = (Get-View $vm.Runtime.Host -Server $esxImpl.Uid.Split('@')[1].Split(':')[0]).Name.Split(".")[0] + "#" + $disk $scsiTab[$key][1] = $vm.Name + "/" + $_.DeviceInfo.Label } } } $scsiTab.GetEnumerator() | Group-Object -Property { $_.Key.Split("#")[1] } | ForEach-Object { $lun = New-Object ("LunInfo" + $rndNum) $lun.ClusterName = $ClusterName $_.Group | ForEach-Object { $esxName = $_.Key.Split("#")[0] $lun.$esxName = $_.Value[3] if (!$lun.CanonicalName) { $lun.CanonicalName = $_.Value[0] } if (!$lun.UsedBy) { $lun.UsedBy = $_.Value[1] } if (!$lun.SizeGB) { $lun.SizeGB = [math]::Round($_.Value[2], 0) } $lun.DSC = $_.Value[4] } $lun } | Export-Csv $csvName -NoTypeInformation -UseCulture Invoke-Item $csvName