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
#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.

#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!

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

$ESXiHosts = Get-VMHost | Sort-Object

foreach ($ESXiHost in $ESXiHosts)
$h = Get-VMhost $ESXiHost.Name
Write-Host "Getting IQN for $h"
$hostview = Get-View $
$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"