Get HBA device info from remote servers and export to CSV

Standard

If you work with SAN storage and fibre channel adapters it can be very useful to get an overview of all your HBAs (host bus adapters) on your servers. This script uses WMI to get HBA info like WWN, driver, version, model etc. from remote servers and then export it to a CSV file. You will then have a consolidated view of all your HBA devices with detailed information about them.
You will need to create the file servers.txt in the script directory and enter the names (one per line) of the servers you want to get the info from.
I have divided this script into three sections variables, functions and script main. I will now briefly explain how I created each section.

Variables

#VARIABLES
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath #path to the directory in which the script is run.
$servers = @(gc "$dir\servers.txt") #create servers.txt in the script directory and enter the names of servers you wish to query for HBA info.
$CSV = "$dir\HBAs.csv" #results will be exported to a csv file with this path

$servers is an array of server names populated by running Get-Content on the file servers.txt. $CSV is the path of the CSV file which will be created at the end of the script run. Both objects $servers and $CSV are defined using the object $dir which always points to the directory which the script was started in. This is practical as it allows me to copy and move the script around without having to change any paths.

Functions

1. Function Get-HBAInfo

#FUNCTIONS
function Get-HBAInfo {
	[CmdletBinding()]
	[OutputType([System.String])]
	param(  
		[parameter(ValueFromPipeline = $true)]$server  
	)	
	process {
		$WMI = Get-WmiObject -class MSFC_FCAdapterHBAAttributes -namespace "root\WMI" -computername $server		
		$WMI | % {
			$HBA = "" | select "server","WWN","DriverName","DriverVersion","FirmwareVersion","Model","ModelDescription"
			$HBA.server = $server
			$HBA.WWN = (($_.NodeWWN) | % {"{0:x}" -f $_}) -join ":"				
			$HBA.DriverName       = $_.DriverName  
			$HBA.DriverVersion    = $_.DriverVersion  
			$HBA.FirmwareVersion  = $_.FirmwareVersion  
			$HBA.Model            = $_.Model  
			$HBA.ModelDescription = $_.ModelDescription
			$HBA
		}			
	}	
}

This function takes a single parameter $server and runs a WMI query on it to get the HBA information. For each HBA device found on the server A custom object called $HBA is created and returned. The function can take input from the pipeline which is practical as you can simply pass the server name to the function using another script or cmdlet if you want.

Script Main

#SCRIPT MAIN
clear
$HBAs = @($servers | Get-HBAInfo)
$HBAs | Export-Csv $CSV -NoTypeInformation -Force

The script main is very simple consisting of only two lines. First I use the Get-HBAInfo function to get the HBA information from the given servers. Then, in the second line, the results are exported to CSV.

I have copied in the full script below. I hope you find it useful. Enjoy!!

################################################################################################
##Script:			Get-HBAInfo.ps1
##
##Description:		Gets information about HBAs on the given servers using WMI and exports it to 
#+					CSV. Remember to create the file servers.txt in the script directory and
#+					enter the names (one per line) of the servers you want to get the info from.
##Created by:		Noam Wajnman
##Creation Date:	February 28, 2013
##Updated:			April 08, 2014
################################################################################################
#FUNCTIONS
function Get-HBAInfo {
	[CmdletBinding()]
	[OutputType([System.String])]
	param(  
		[parameter(ValueFromPipeline = $true)]$server  
	)	
	process {
		$WMI = Get-WmiObject -class MSFC_FCAdapterHBAAttributes -namespace "root\WMI" -computername $server		
		$WMI | % {
			$HBA = "" | select "server","WWN","DriverName","DriverVersion","FirmwareVersion","Model","ModelDescription"
			$HBA.server = $server
			$HBA.WWN = (($_.NodeWWN) | % {"{0:x}" -f $_}) -join ":"				
			$HBA.DriverName       = $_.DriverName  
			$HBA.DriverVersion    = $_.DriverVersion  
			$HBA.FirmwareVersion  = $_.FirmwareVersion  
			$HBA.Model            = $_.Model  
			$HBA.ModelDescription = $_.ModelDescription
			$HBA
		}			
	}	
}
#VARIABLES
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath #path to the directory in which the script is run.
$servers = @(gc "$dir\servers.txt") #create servers.txt in the script directory and enter the names of servers you wish to query for HBA info.
$CSV = "$dir\HBAs.csv" #results will be exported to a csv file with this path
#SCRIPT MAIN
clear
$HBAs = @($servers | Get-HBAInfo)
$HBAs | Export-Csv $CSV -NoTypeInformation -Force
Advertisements

12 thoughts on “Get HBA device info from remote servers and export to CSV

  1. Hi Noam, awesome work on the powershell script !!! it works like charm.
    Do you know any WMI could use to gather the HBA port speed? and the MPIO Policy setting?

    I have try various MSFC_* WMI classes and it is not returning any instances. Thanks.

    • Hi Eugene,
      Try running these commands:
      gwmi -namespace “root\wmi” -class MSFC_FCAdapterHBAAttributes -ComputerName

      gwmi -namespace “root\wmi” -class MSFC_FibrePortHbaAttributes -ComputerName

      These should give you the information you need.

      • Hi Noam,

        I have got the hba card info with MSFC_FCAdapterHBAAttributes, and MSFC_FibrePortHbaAttributes gives the following details but not manage to get the HBA port link speed.

        Do you know any way to obtain the HBA port link speed and the Disk drives MPIO policy

        Active : True
        Attributes :
        HBAStatus : 0
        InstanceName : PCI\VEN_10DF&DEV_F100&SUBSYS_3282103C&REV_03\4&32dd9a56&0&0050_0
        UniquePortId : 18446738026558816264

        Thanks again.

  2. PC_Doctor

    This really fits the bill! I had to modify the script because I have an automated process that generates a list of Hosts connected to my SAN infrastructure, some being Linux and VMWare. Fortunately, the FQDN for the Windows hosts all have “.FOO.” in them. I also added a line to check for connectivity before running the WMI Query.

    #FUNCTIONS
    function Get-HBAInfo {
    [CmdletBinding()]
    [OutputType([System.String])]
    param(
    [parameter(ValueFromPipeline = $true)]$server
    )
    process {
    $WMI = Get-WmiObject -class MSFC_FCAdapterHBAAttributes -namespace “root\WMI” -computername $server
    $WMI | % {
    $HBA = “” | select “Server”,”WWN”,”DriverName”,”DriverVersion”,”FirmwareVersion”,”Model”,”ModelDescription”
    $HBA.server = $server
    $HBA.WWN = (($_.NodeWWN) | % {“{0:x}” -f $_}) -join “:”
    $HBA.DriverName = $_.DriverName
    $HBA.DriverVersion = $_.DriverVersion
    $HBA.FirmwareVersion = $_.FirmwareVersion
    $HBA.Model = $_.Model
    $HBA.ModelDescription = $_.ModelDescription
    $HBA
    }
    }
    }
    #VARIABLES
    $scriptpath = $MyInvocation.MyCommand.Path
    $dir = Split-Path $scriptpath #path to the directory in which the script is run.
    $servers = @(gc “$dir\hosts.txt”) #create servers.txt in the script directory and enter the names of servers you wish to query for HBA info.
    $CSV = “$dir\HBAs.csv” #results will be exported to a csv file with this path
    HBAs = @()
    #SCRIPT MAIN
    clear
    Foreach($Server in $Servers){
    if ($Server.TOUpper() -like “*.FOO.*”) {
    if(Test-Connection -ComputerName $Server -Count 1 -ea 0) {
    Write-host $Server
    $HBAs = $HBAs + (Get-HBAInfo $server)
    }
    }
    }
    $HBAs | Export-Csv $CSV -NoTypeInformation -Force

    • nice work man! However I recommend that you check out the script I have to test for open ports on remote servers. It works much faster than pinging with test-connection especially when you are working with many servers.

  3. Ajit

    Hi Noam,

    I’m trying to run the script in server 2012 but it is not working. error i’m getting is the “Get-wmiobject : not supported” is there some pre-configuration that is required on server2012 to acces s class MSFC_FCAdapterAttributes??

  4. Praveen.S

    Hi Noam,

    the script works for windows 2008 and Later. however the script is not working for windows 2003 server.

    Please let me know how to proceed in further for windows 2003 server.

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