Get HBA WWN for Hosts using Powercli

Have a need to get the WWN of all your HBA’s to present storage?  Here is a little one liner that can assist with that and will convert it to hex as well! The single command below will not give you pretty colons in the WWN output though. To do that, see my function below to get a nice spreadsheet. You can pipe any object like Get-Datacenter or Get-Cluster to the command below to specify what hosts you need specifically:

#Get WWN for cluster 
Get-Cluster clustername | Get-VMhost | Get-VMHostHBA -Type FibreChannel | Select VMHost,Device,@{N="WWN";E={"{0:X}" -f $_.PortWorldWideName}} | Sort VMhost,Device

#Get WWN by single host 
Get-VMhost -Name Host | Get-VMHostHBA-Type FibreChannel | Select VMHost,Device,@{N="WWN";E={"{0:X}" -f $_.PortWorldWideName}} | Sort VMhost,Device

I also created a script to gather based on cluster, will format the output to include colons, and output to csv name you specify.

#Set mandatory parameters for cluster and csvname
param (
[Parameter(Mandatory=$true)][string]$Cluster,
[Parameter(Mandatory=$true)][string]$CSVName
)

#Get cluster and all host HBA information and change format from Binary to hex
$list = Get-cluster $cluster | Get-VMhost | Get-VMHostHBA -Type FibreChannel | Select VMHost,Device,@{N="WWN";E={"{0:X}" -f $_.PortWorldWideName}} | Sort VMhost,Device

#Go through each row and put : between every 2 digits
foreach ($item in $list){
   $item.wwn = (&{for ($i=0;$i -lt $item.wwn.length;$i+=2)   
                    {     
                        $item.wwn.substring($i,2)   
                    }}) -join':' 
}

#Output CSV to current directory.
$list | export-csv -NoTypeInformation $CSVName

Finally, here is a function where you can pass a datacenter or cluster object and enter a csv file name.

Usage:

Get-Datacenter | Get-WWN -csv filename
function Get-WWN {    
    #Set mandatory parameters for cluster and csvnameparam 
    ([CmdletBinding()]
    [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelinebyPropertyName=$True)]
    $VMObject,
    [Parameter(Mandatory=$true)]
    [string]$csv
    )

    #Get cluster and all host HBA information and change format from Binary to hex
$list = $VMObject | Get-VMhost | Get-VMHostHBA -Type FibreChannel | Select VMHost,Device,@{N="WWN";E={"{0:X}" -f $_.PortWorldWideName}} | Sort VMhost,Device

    #Go through each row and put : between every 2 digits
foreach ($item in $list){
       $item.wwn = (&{for ($i=0;$i -lt $item.wwn.length;$i+=2)   
                        {     
                            $item.wwn.substring($i,2)   
                        }}) -join':' 
    }

    #Output CSV to current directory.
$list | export-csv -NoTypeInformation $csv.csv

}
Advertisements

4 Responses to Get HBA WWN for Hosts using Powercli

  1. Thanks (used this today) for the one-liners and scripts. Been looking for this for ever on how to do this in powercli. Hopefully 5.5 PowerCLI has this.

    -Rich

  2. vineeth says:

    Hi

    Thanks for this script. This is very useful but i need output as below in .csv can you help me out.

    Datacenter : testing

    Cluster : testing

    VMHost : testing1.test.com

    Device :vmhba1

    WWN — AB:CD:EF:GH:IJ:KL:NM:OP

    Datacenter : testing

    Cluster : testing

    VMHost : testing1.test.com

    Device : vmhba2

    WWN — QR:ST:UV:WX:YZ:AB:CD:EF

    • markdjones82 says:

      Try this Here is the one that will list datacenter/cluster. It isn’t a super efficient script, but it works.
      Usage: Get-Datacenter | Get-WWN

      function Get-WWN {
      #Set mandatory parameters for cluster and csvnameparam
      ([CmdletBinding()]
      [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelinebyPropertyName=$True)]
      $VMObject,
      [Parameter(Mandatory=$true)]
      [string]$csv
      )

      #Get cluster and all host HBA information and change format from Binary to hex
      $list = $VMObject | Get-VMhost | Get-VMHostHBA -Type FibreChannel | Select @{N=”Datacenter”;E={get-datacenter -vmhost $_.vmhost[0]}}, @{N=”Cluster”;E={(get-vmhost $_.vmhost[0]).parent.name}},VMHost,Device,@{N=”WWN”;E={“{0:X}” -f $_.PortWorldWideName}} | Sort VMhost,Device

      #Go through each row and put : between every 2 digits
      foreach ($item in $list){
      $item.wwn = (}) -join’:’
      }

      #Output CSV to current directory.
      $list | export-csv -NoTypeInformation $csv.csv

      }

  3. Pingback: WWN Powercli quick and easy | vToote

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

%d bloggers like this: