Get-IQN script for getting all IQNs of managed ESXi hosts

Because of an error in the past, I wanted to make sure that all IQNs were stored in our CMDB and available at times of disaster.

I created a script for this, which will get the IQNs for all ESXi hosts under your vCenter server and export the information to a CSV file. It can be downloaded from here or you can use the text below and save it as *.ps1 file.

–Update on 9th of May, 2014: Keith (bouldergeek) supplied code that works with PowerCLI 5.5, check it out in the comments. Thanks for your feedback Keith!

#General information
#Date: May 24th, 2013
#Author: Rene Bos
#URL: http://snowvm.com
#Script version: 0.1

#Script summary
#Tested on vCenter Server and ESXi 5.1
#This script will retreive the IQNs of all ESXi hosts under your vCenter Server and will export this information in a table-like format to a CSV file.
#You need VMware PowerCLI installed on your machine and an active connection to one or more vCenter servers.

#Disclaimer
#Please use this script at your own risk and test it out in your testlab first before using it in production
#When using my script, please leave the general information in place. And let me know if my scripts needs improvement!

#Customizations
$CsvPath = "C:TempIQN-Export.csv"

#Variables
$ESXiHosts = Get-VMHost | Sort-Object

foreach ($ESXiHost in $ESXiHosts)
{
$h = Get-VMhost $ESXiHost.Name
Write-Host "Getting IQN for $h"
$hostview = Get-View $h.id
$storage = Get-View $hostview.ConfigManager.StorageSystem

$a = [PSCustomObject]@{
Hostname = $h.Name
IQN = $storage.StorageDeviceInfo.HostBusAdapter.iScsiName
}
$a | Export-CSV -Path $CsvPath -Append
}
Write-Host "CSV exported to $CsvPath"

2 thoughts on “Get-IQN script for getting all IQNs of managed ESXi hosts

  1. Great script. Unfortunately under pCLI 5.5 it doesn’t like the -Append parameter. So, I was reworking it to stuff them into a custom collection, and it occurred to me there might be an easier method. Behold:

    Get-VMhost | Get-VMHostHba -Type IScsi | Select VMHost,Device,IScsiName | Sort-Object VMHost | Out-GridView

    This pipes it to a pop-up grid you can copy/paste from as desired. You could just as easily replace Out-GridView with the Export-CSV instead if you still want it in a file. Cheers!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s