Just something I wanted to share with the community…
One of our VMWare Administrators was working with VMWare vendor support in looking at some SAN connection issues and was requesting a report showing the following information: Host, Name, HBAName, Source, Target, LUN, Status, Path, PathSelectionPolicy
They could then use this information to further review the data and make some recommendations on our infrastructure. While you can view the data by looking at vSphere and going through a couple of different areas to pull the data, hand copying this data would be just painfully slow.
Here is where PowerShell and PowerCLI come into play. I was asked if I could put a script together that could pull all of the information and put it into a report of some sort. Putting the data into a report is very simple using Export-CSV, but gathering the data would be a little trickier as the required data requested was not in one place to just pull from.
Fortunately, after a little exploring I was able to locate all of the data and put together a short 41 line script that would pull this data and put it into a CSV file for the vendor to use.
A couple of things I added to the script was the use of Write-Progress to allow you to see where the script was at in its processing. Otherwise, you could be left wondering for an hour or so trying to figure out if the script was still running of was stuck somewhere.
In my case, the script took about an hour to run in one of my environments and produced a 11000+ row CSV file with the information seen below.
While I am by no means an expert with PowerCLI and am sure that there may be a better way of pulling this data, this still worked out like a champ and provided the vendor the useful information to decide where some changes could be made at. I would not call it a “finished” script as it lacks inline help and verbose logging, but feel free to take it and make any additions to it that you see fit. Hopefully you will find this script just as useful in your environment.
Remember to update the vCenter Server in the script to point to your server so the script works correctly!
Download (remove .doc from file)
Source Code
#Connect to vCenter Server
Connect-VIServer vCenterServer
#Get list of ESXi Hosts
$esxihosts = Get-VMHost
$i=0
$data = ForEach ($esxi in $esxihosts) {
$i++
Write-Progress -Activity "Scanning hosts" -Status ("Host: {0}" -f $esxi.Name) -PercentComplete ($i/$esxihosts.count*100) -Id 0
$hbas = $esxi | Get-VMHostHba
$j=0
ForEach ($hba in $hbas) {
$j++
Write-Progress -Activity "Scanning HBAs" -Status ("HBA: {0}" -f $hba.Device) -PercentComplete ($j/$hbas.count*100) -Id 1
$scsiluns = $hba | Get-ScsiLun
$k=0
ForEach ($scsilun in $scsiluns) {
$k++
Write-Progress -Activity "Scanning Luns" -Status ("Lun: {0}" -f $scsilun.CanonicalName) -PercentComplete ($k/$scsiluns.count*100) -Id 2
$scsipaths = $scsilun | Get-Scsilunpath
$l=0
ForEach ($scsipath in $scsipaths) {
$l++
Write-Progress -Activity "Scanning Paths" -Status ("Path: {0}" -f $scsipath.Name) -PercentComplete ($l/$scsipaths.count*100) -Id 3
New-Object PSObject -Property @{
Host = $esxi.name
HBAName = $scsilun.RuntimeName
PathSelectionPolicy = $scsilun.MultiPathPolicy
Status = $scsipath.state
Source = "{0}" -f ((("{0:x}" -f $hba.PortWorldWideName) -split '([a-f0-9]{2})' | where {$_}) -Join ":")
Target = $scsipath.SanId
LUN = (($scsilun.RunTimeName -Split "L")[1] -as [Int])
Path = $scsipath.LunPath
}
}
}
}
}
$data | Export-Csv -NoTypeInformation 'ESXiStorageInfo.csv'
