Get ESXi Hosts WWNs Powershell and Shell Scripts

So, at first you’d say oh this has been done before via PowerCLI and that is true in the case where you have a vCenter up and running and you’ve created all your clusters and so on and so forth.

But my argument is, when we build new infrastructures (and I am talking to the guys whom are like me running a one man show in the project from top to bottom ;-)) we install ESXi and get the hosts ready and now we’d want to connect them to the fabric so that to get our datastores and start building the virtual machines from that point and when we’re using SD cards for the ESXi it doesn’t leave much of an option to install a vCenter Server on the local datastore and run a PowerCLI script.

So usually, I would go to each host and run (esxcli storage san fc/fcoe/iscsi list) and I would copy the requested information in order to prepare for the zoning configuration on the FC switches, and recently I was involved in a project and I had a lot of hosts to cover so I went back home for the weekend and wrote the hereunder script/s.

Hopefully this will be useful and will save you sometime and some copy and paste operations =)…

Description and prerequisites:

The script consists of two files (GetVMHBA-PS.ps1 and GETVMHBA.sh) where the powershell script runs and triggers a putty session that will run the shell script on each ESXi host.

  1. Putty.exe with logging enabled on default settings and set to %H.log
  2. Powershell with Set-ExecutionPolicy set correctly to allow the script to run.
  3. SSH Enabled on all ESXi hosts.
  4. Root password for all ESXi hosts is the same (it should be since this is new installation).
  5. HBA type on the ESXi hosts (i.e fc/iscsi/fcoe).
  6. You can download a zip archive of the script from here and I’ve included putty with it as well.

My preferable way of doing it: I created a directory called VMHBAScript and put everything in it including putty.exe and set the default settings for logging and set the log name to %H.log (which will output hostname.log or ipofhost.log) and ran the script from there.

GetVMHBA-PS.ps1:

<pre># This script is intended to get the SAN HBAs information from ESXi hosts in order to facilitate the process of
# zoning, host creation and LUN assignment on SAN arrays.
#
# Abdullah Abdullah | https://notes.doodzzz.net | @do0dzZZ
#
# Created on 29/03/2015
# Last updated on 30/03/2015
#
# You may alter the script in any way you see fit, just make sure you share it if you've made a major enhancement!
#####
#Script Starts hereunder!
#####
# Path for VMHBA Script Directory
$PathtoVMHBADir = (Read-Host "Enter the path to the VMHBA script i.e C:\VMHBA\:")
cd "$PathtoVMHBADir"

# Get the IP addresses of the ESXi hosts you want to get their WWN
[array]$ESXiHosts = (Read-Host “Enter the IP addresses of the ESXi hosts (SEPARATE with a COMMA)”).split(“,”) | %{$_.trim()} 

# Type of FC adapter on the ESXi hosts
$TypeOfAdapter = (Read-Host "Enter type of adapter (FC, iSCSI or FCoE):")

# ESXi hosts credentials
$ESXiUserName = (Read-Host "Enter ESXi root:")
$ESXiPassword = (Read-Host -assecurestring "Enter ESXi root password:")
$UsableESXiPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($ESXiPassword))

Write-Host "Okay we're good to go [Creating script and invoking it on hosts]" -BackgroundColor Yellow -ForegroundColor Black
# Generate the script based on the HBA type
copy .\GETVMHBA.sh .\GETVMHBA$TypeofAdapter.sh
(Get-Content GETVMHBA$TypeofAdapter.sh | ForEach-Object { $_ -replace "ReplaceMe", "$TypeofAdapter" } ) | Set-Content GETVMHBA$TypeofAdapter.sh

# Running the shell script on the ESXi hosts
for($i=0; $i -lt $ESXiHosts.Length; $i++)
{
    $PUTTYExecutable = &.\putty.exe -ssh $ESXiHosts[$i] -l $ESXiUserName -pw $UsableESXiPassword -m GETVMHBA$TypeofAdapter.sh | Wait-Process
    }
 
# Showing the extracted WWNs for all hosts
$Hosts = Get-Item $PathtoVMHBADir\*.log
$hostcount = 1
foreach ($hosts in $hosts)
{
        Select-String $hosts -Pattern  vmhba
        Write-Host "Finished listing ESXi Host $hostcount HBAs" -BackgroundColor Black -ForegroundColor Yello -NoNewline
        Write-Host "====================================================" -BackgroundColor Yellow -ForegroundColor Black
        $hostcount = $hostcount + 1

  }

GETVMHBA.sh:

vmhbacount=$(esxcli storage san ReplaceMe list | grep vmhba | wc -l)
i=0
while [ $i -lt $vmhbacount ]
do
printf "`hostname` ; `esxcli storage san fc list -A vmhba$i | egrep Adapter` ; `esxcli storage san fc list -A vmhba$i | egrep Node`\n"
i=`expr $i + 1`
done

And the end result should look something like this:
GetVMHBA-Screenshot

I hope I’ve helped ^_^.
(Abdullah)^2

5343 Total Views 3 Views Today

Abdullah

Knowledge is limitless.

Leave a Reply

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