Automation

 View Only
Expand all | Collapse all

powercli script to get targets devices paths from HBA

  • 1.  powercli script to get targets devices paths from HBA

    Posted Nov 22, 2010 11:15 PM

    I need to quickly check all the hosts in my clusters to see all of them have the correct number of HBA targets, devices and paths. I can see this information in vCenter. Not an easy task if you have 100+ hosts. Does anyone have a PowerCLI script to get this info?



  • 2.  RE: powercli script to get targets devices paths from HBA

    Posted Nov 23, 2010 07:12 AM

    Try this

    $esx = Get-VMHost <hostname>
    
    foreach($hba in (Get-VMHostHba -VMHost $esx -Type "FibreChannel")){
    	$target = ($hba.VMhost.ExtensionData.Config.StorageDevice.ScsiTopology.Adapter | where {$_.Adapter -eq $hba.Key}).Target
    	$luns = Get-ScsiLun -Hba $hba  -LunType "disk"
    	$nrPaths = ($target | %{$_.Lun.Count} | Measure-Object -Sum).Sum
    
    	Write-Host $hba.Device "Targets:" $target.Count "Devices:" $luns.Count "Paths:" $nrPaths
    }
    

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 3.  RE: powercli script to get targets devices paths from HBA

    Posted Nov 23, 2010 01:39 PM

    Thanks LucD for the quick update, and it worked. You will have to use PowerCLI 4.1. Didn't work in 4.0



  • 4.  RE: powercli script to get targets devices paths from HBA
    Best Answer

    Posted Nov 23, 2010 01:50 PM

    For PowerCLI builds before 4.1 it only needs a small adaption.

    $esx = Get-VMHost <hostname>
    
    foreach($hba in (Get-VMHostHba -VMHost $esx -Type "FibreChannel")){
    	$target = ((Get-View $hba.VMhost).Config.StorageDevice.ScsiTopology.Adapter | where {$_.Adapter -eq $hba.Key}).Target
    	$luns = Get-ScsiLun -Hba $hba  -LunType "disk"
    	$nrPaths = ($target | %{$_.Lun.Count} | Measure-Object -Sum).Sum
    
    	Write-Host $hba.Device "Targets:" $target.Count "Devices:" $luns.Count "Paths:" $nrPaths
    }
    

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 5.  RE: powercli script to get targets devices paths from HBA

    Posted Oct 14, 2013 12:06 AM

    Luc,

    Is there any way to show the Host name on the script result ?

    because the result is hard to understand with the following:

    vmhba1 Targets: 2 Devices: 80 Paths: 160

    vmhba2 Targets: 2 Devices: 80 Paths: 160

    vmhba1 Targets: 2 Devices: 80 Paths: 160

    vmhba2 Targets: 2 Devices: 80 Paths: 160

    vmhba1 Targets: 2 Devices: 80 Paths: 160

    vmhba2 Targets: 2 Devices: 80 Paths: 160

    vmhba1 Targets: 2 Devices: 8 Paths: 16

    vmhba2 Targets: 2 Devices: 8 Paths: 16

    vmhba1 Targets: 2 Devices: 8 Paths: 16

    vmhba2 Targets: 2 Devices: 8 Paths: 16

    vmhba3 Targets: 2 Devices: 8 Paths: 16

    vmhba4 Targets: 2 Devices: 8 Paths: 16

    ...

    Thanks in advance.



  • 6.  RE: powercli script to get targets devices paths from HBA

    Posted Mar 25, 2014 05:43 PM

    Use this for esx 5 and above. pretty fast

    param($VMHost)

    Get-VMHost $VMHost | % {

                $VMHost =$_

    $details= (Get-EsxCli -VMHost $VMHost).storage.core.path.list() | ? DeviceDisplayName -like "*Fibre Channel*"

    $vmhba0 = $details | ? Adapter -like "*vmhba0*" | measure-object | select Count

    $vmhba1 = $details | ? Adapter -like "*vmhba1*" | measure-object | select Count

    $vmhba2 = $details | ? Adapter -like "*vmhba2*" | measure-object | select Count

    $vmhba3 = $details | ? Adapter -like "*vmhba3*" | measure-object | select Count

    $vmhba4 = $details | ? Adapter -like "*vmhba4*" | measure-object | select Count

    New-Object PSObject |

    Add-Member -pass NoteProperty Name                               $VMHost.Name          |

    Add-Member -pass NoteProperty Version                               $VMHost.Version          |

    Add-Member -pass NoteProperty Build                               $VMHost.build          |

    Add-Member -pass NoteProperty Paths                       $details.count     |

    Add-Member -pass NoteProperty vmhba0                      $vmhba0.count     |

    Add-Member -pass NoteProperty vmhba1                      $vmhba1.count     |

    Add-Member -pass NoteProperty vmhba2                      $vmhba2.count     |

    Add-Member -pass NoteProperty vmhba3                      $vmhba3.count     |

    Add-Member -pass NoteProperty vmhba4                      $vmhba4.count

    } | Sort Name |ft -a

    Output

    Name                         Version Build   Paths vmhba0 vmhba1 vmhba2 vmhba3 vmhba4

    ----                         ------- -----   ----- ------ ------ ------ ------ ------

    meda0101esx.meda 5.1.0   1483097   880 220    220    220 220      0

    meda0102esx.meda 5.1.0   1483097   880 220    220    220 220      0

    meda0103esx.meda 5.1.0   1483097   880 220    220    220 220      0

    meda0104esx.meda 5.1.0   1483097   880 220    220    220 220      0

    meda0105esx.meda 5.1.0   1483097   880 220    220    220 220      0

    meda0106esx.meda 5.1.0   1483097   880 220    220    220 220      0



  • 7.  RE: powercli script to get targets devices paths from HBA

    Posted Nov 23, 2010 07:21 AM

    The next script will give you the number of targets, devices and paths of all the HBA's of all your hosts:

    Get-VMHost | Sort-Object -property Name | ForEach-Object {
      $VMHost = $_
      $VMHost | Get-VMHostHba | Sort-Object -property Device | ForEach-Object {
        $VMHostHba = $_
        $ScsiLun = $VMHostHba | Get-ScsiLun
        If ($ScsiLun) {
          $ScsiLunPath = $ScsiLun | Get-ScsiLunPath | `
            Where-Object {$_.Name -like "$($VMHostHba.Device)*"}
          $Targets = ($ScsiLunPath | `
            Group-Object -Property SanID | Measure-Object).Count
          $Devices = ($ScsiLun | Measure-Object).Count
          $Paths = ($ScsiLunPath | Measure-Object).Count
        }
        Else {
          $Targets = 0
          $Devices = 0
          $Paths = 0
        }
        $Report = "" | Select-Object -Property VMHost,HBA,Targets,Devices,Paths
        $Report.VMHost = $VMHost.Name
        $Report.HBA = $VMHostHba.Device
        $Report.Targets = $Targets
        $Report.Devices = $Devices
        $Report.Paths = $Paths
        $Report
      }
    }
    

    Regards, Robert



  • 8.  RE: powercli script to get targets devices paths from HBA

    Posted Nov 23, 2010 01:42 PM

    Thank you Robert.

    The Target and Paths were blank. The Devices reported correctly. Thanks again for the reply.



  • 9.  RE: powercli script to get targets devices paths from HBA

    Posted Nov 23, 2010 05:46 PM

    My scripts works good in my environment with PowerCLI 4.1. Maybe it doesn't work with PowerCLI 4.0 or earlier. In that case you better use Luc's script. :smileywink:



  • 10.  RE: powercli script to get targets devices paths from HBA

    Posted Nov 23, 2010 07:29 PM

    Robert, I think the problem is with this line (I get it as well with PowerCLI 4.1).

    $ScsiLunPath = $ScsiLun | Get-ScsiLunPath | `
        Where-Object {$_.Name -like "$($VMHostHba.Device)*"}
    

    The name of the path doesn't always start with the HBA name.

    If you do

    $ScsiLunPath = $ScsiLun | Get-ScsiLunPath | `
        Where-Object {$_.Name -like "*$($VMHostHba.Device)*"}
    

    or

    $ScsiLunPath = $ScsiLun | Get-ScsiLunPath | `
        Where-Object {$_.Name.Contains($VMHostHba.Device)}
    

    your script will work, except for iScsi LUNs where the path doesn't contain the HBA name at all.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 11.  RE: powercli script to get targets devices paths from HBA

    Posted Nov 23, 2010 10:30 PM

    You guys rocks. Love the community.

    Happy Holidays!



  • 12.  RE: powercli script to get targets devices paths from HBA

    Posted Dec 07, 2010 02:16 PM

    The script worked great! Thank you very much for this!!!

    I'm new to powershell and could use a bit of help making a small modification. I'd like to target just a cluster instead of the entire infrastructure. I am trying to get something like this working:

    foreach ($VMHost in get-cluster

    Would anyone happen to have some ideas how to do it?

    Thanks!



  • 13.  RE: powercli script to get targets devices paths from HBA

    Posted Dec 07, 2010 02:42 PM

    Hi Todd,

    you can do the following to select only the hosts in one cluster:

    Get-Cluster <CLUSTERNAME> | Get-VMHost | Sort-Object -property Name | ForEach-Object) {
      $VMHost = $_
      ...
    }
    
    

    Regards, Robert



  • 14.  RE: powercli script to get targets devices paths from HBA

    Posted Dec 07, 2010 04:23 PM

    Okay that worked! However I just noticed that the original and new script does not display the Targets and Paths for an ESXi 4.0 host. It works great on a 3.5 host. I'm using PowerCLI 4.1. Any ideas?

    Thanks!



  • 15.  RE: powercli script to get targets devices paths from HBA

    Posted Oct 11, 2013 02:02 PM

    does this script works for powerCLI version 5.1 ?



  • 16.  RE: powercli script to get targets devices paths from HBA

    Posted Oct 11, 2013 06:49 PM

    Hi Albert,

    its work also on 5.1



  • 17.  RE: powercli script to get targets devices paths from HBA

    Posted Oct 14, 2013 12:01 AM

    Rob,

    Thanks for the script, but is there any reason as to why the targets and paths is shown as 0 ?

    VMHost  : PRODESXi01.domain.com

    HBA     : vmhba0

    Targets : 0

    Devices : 80

    Paths   : 0

    VMHost  : PRODESXi01.domain.com

    HBA     : vmhba1

    Targets : 0

    Devices : 80

    Paths   : 0

    VMHost  : PRODESXi02.domain.com

    HBA     : vmhba0

    Targets : 0

    Devices : 80

    Paths   : 0

    VMHost  : PRODESXi02.domain.com

    HBA     : vmhba0

    Targets : 0

    Devices : 80

    Paths   : 0

    my PowerCLI version is as follows:

    PowerCLI Version

    ----------------

       VMware vSphere PowerCLI 5.1 Release 2 build 1012425

    ---------------

    Snapin Versions

    ---------------

       VMWare AutoDeploy PowerCLI Component 5.1 build 768137

       VMWare ImageBuilder PowerCLI Component 5.1 build 768137

       VMware License PowerCLI Component 5.1 build 669840

       VMware VDS PowerCLI Component 5.1 build 1012428

       VMware vSphere PowerCLI Component 5.1 build 1012428

    and also the format is not in table format, it would be great if the output is in the table or in CSV if possible.



  • 18.  RE: powercli script to get targets devices paths from HBA

    Posted Apr 20, 2023 07:56 PM

    tested working on ESXi 6.7, 7.x



  • 19.  RE: powercli script to get targets devices paths from HBA

    Posted Jul 15, 2013 06:26 AM

    hi LucD & Rob

    any updates if i run it on ESXi 5??



  • 20.  RE: powercli script to get targets devices paths from HBA

    Posted Jul 15, 2013 06:32 AM

    Do you get any errors when you run it in a vSphere 5 environment ?



  • 21.  RE: powercli script to get targets devices paths from HBA

    Posted Jul 15, 2013 06:36 AM

    yes:

    Get-ScsiLun : 15/07/2013 09:01:01    Get-ScsiLun        Value cannot be null.

    Parameter name: array   

    At line:5 char:14

    +      $luns = Get-ScsiLun -Hba $hba  -LunType "disk"

    +              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [Get-ScsiLun], VimException

        + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.GetScsiLun

    Get-ScsiLun : 15/07/2013 09:01:01    Get-ScsiLun        ScsiLun with luntype 'disk' was not found using the specified filter

    (s).   

    At line:5 char:14

    +      $luns = Get-ScsiLun -Hba $hba  -LunType "disk"

    +              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : ObjectNotFound: (:) [Get-ScsiLun], VimException

        + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.GetScs

       iLun

    i guess its somthing about the EMC PowerPath

    please advice?



  • 22.  RE: powercli script to get targets devices paths from HBA

    Posted Jul 15, 2013 07:26 AM

    small update

    Robert script work fine on  vSphere 5 environment but didnt get Targets and Paths number

    only devices




  • 23.  RE: powercli script to get targets devices paths from HBA

    Posted Jul 15, 2013 07:37 AM

    There used to be indeed a problem with the Get-ScsiLun cmdlet when PowerPath was installed.

    Which PowerCLI version are you using ? Do a

    Get-PowerCLIVersion



  • 24.  RE: powercli script to get targets devices paths from HBA

    Posted Jul 15, 2013 07:47 AM

    PowerCLI Version

    ----------------

       VMware vSphere PowerCLI 5.1 Release 2 build 1012425

    ---------------

    Snapin Versions

    ---------------

       VMWare AutoDeploy PowerCLI Component 5.1 build 768137

       VMWare ImageBuilder PowerCLI Component 5.1 build 768137

       VMware vCloud Director PowerCLI Component 5.1 build 1012427

       VMware License PowerCLI Component 5.1 build 669840

       VMware VDS PowerCLI Component 5.1 build 1012428

       VMware vSphere PowerCLI Component 5.1 build 1012428



  • 25.  RE: powercli script to get targets devices paths from HBA

    Posted Jul 15, 2013 07:49 AM

    That's the latest version.

    So it seems that the PowerPath "feature" might still be there I'm afraid.



  • 26.  RE: powercli script to get targets devices paths from HBA

    Posted Jul 15, 2013 07:55 AM

    there is any way to pass it?



  • 27.  RE: powercli script to get targets devices paths from HBA

    Posted Jul 15, 2013 08:07 AM

    i see in the powercli realse notes the issue

    Get-ScsiLun

      • When you use Get-ScsiLun to retrieve Powerpath devices, the value of their  MultipathPolicy property is shown as Unknown

    i try to create some batch file  or PS1 from the rpowermt side

    i will update for all EMC PP\VE users :smileywink:



  • 28.  RE: powercli script to get targets devices paths from HBA

    Posted Jul 15, 2013 11:16 AM

    ok guys

    bad news :smileysad:

    from PPVE side you can see only one host scsi info

    PS C:\Users\liavk> rpowermt display host=myesxhost

    CLARiiON logical device count=108

    ==============================================================================

    ----- Host Bus Adapters ---------  ------ I/O Paths -----  ------ Stats ------

    ###  HW Path                       Summary   Total   Dead  IO/Sec Q-IOs Errors

    ==============================================================================

       1 vmhba1                        optimal     216      0       -     0      0

       2 vmhba0                        optimal     216      0       -     0      0

    i wish on next powercli version they get over the PowerPath "feature"



  • 29.  RE: powercli script to get targets devices paths from HBA

    Posted Feb 12, 2015 11:50 AM

    Sorry to revive an old thread but I am trying to adapt this script so that I can export it to a CSV in a table format for all of my hosts.  I have borrowed parts of another script I had that I thought created an object that could be exported to CSV but I am not getting anything on the output and the CSV created is empty.  Can anyone see what is going wrong?  Feel free to laugh at my attempt if it was never going to work (as long as you provide a solution ;-)

    ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {

           ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {

                  ForEach ($esx in ($Cluster | Get-VMhost | Sort-Object -Property Name)) {

                     foreach($hba in (Get-VMHostHba -VMHost $esx -Type "FibreChannel")){

                          $target = ($hba.VMhost.ExtensionData.Config.StorageDevice.ScsiTopology.Adapter | where {$_.Adapter -eq $hba.Key}).Target

                          $luns = Get-ScsiLun -Hba $hba

                          $nrPaths = ($target | %{$_.Lun.Count} | Measure-Object -Sum).Sum

                          Select-Object -Property @{N="Cluster";E={$esx.parent.name}},

                          @{N="Host";E={$esx.name}},

                          @{N="HBA";E={$hba.device}},

                          @{N="Targets";E={$target.count}},

                          @{N="Devices";E={$luns.count}},

                          @{N="Paths";E={$nrpaths}}

                          }

                     }

           }

    }

    Cheers



  • 30.  RE: powercli script to get targets devices paths from HBA

    Posted Feb 12, 2015 05:39 PM

    The reason you don't see anything in the CSV file is because the Foreach doesn't place anything on the pipeline.

    You can fix that by using the call operator (&).

    &{

    # Your script

    } | Export-Csv report.csv -NoTypeInformation -UseCulture



  • 31.  RE: powercli script to get targets devices paths from HBA

    Posted Feb 13, 2015 03:04 AM

    yes, you are right, the result in the CSV file is empty.



  • 32.  RE: powercli script to get targets devices paths from HBA

    Posted Apr 06, 2015 06:23 PM

    What script did you end up using?  Also looking to inventory our targets, devices, and paths before storage changes this weekend.



  • 33.  RE: powercli script to get targets devices paths from HBA

    Posted Jan 20, 2016 10:14 AM

    Hi,

    Some environmental info:

    ESXi and vCenter version: 5.0

    Powershell:

    Major  Minor  Build  Revision

    -----  -----  -----  --------

    4      0      -1     -1

    PowerCLI Version:

    VMware vSphere PowerCLI 6.0 Release 1 build 2548067

    I came to this same requirement. I tried to improved the codes in this thread and came up with this:

    $vCenter = Read-Host "vCenter IP "

    $outputFile = "c:\temp\FCPathCount-of-Hosts-in-$vCenter_" + (get-date -Format yyyy-MM-dd-HHmm) + ".csv"

    Connect-VIServer $vCenter

    $report = @()

    ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) { 

        ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {  

            ForEach ($esx in ($Cluster | Get-VMhost | Sort-Object -Property Name)) { 

                foreach($hba in (Get-VMHostHba -VMHost $esx -Type FibreChannel)){ 

                        $target = ($hba.VMhost.ExtensionData.Config.StorageDevice.ScsiTopology.Adapter | where {$_.Adapter -eq $hba.Key}).Target 

                        $luns = Get-ScsiLun -Hba $hba

                        $nrPaths = ($target | %{$_.Lun.Count} | Measure-Object -Sum).Sum

                        $row = "" | select Cluster, VMHost, vmhba, Targets, Devices, Paths

                        $row.Cluster = $Cluster.Name

                        $row.VMHost = $esx.Name

                        if ($hba.ExtensionData.Status -eq "online") {

                        if ($row.vmhba -eq $null) {

                            $row.vmhba = $hba.Device

                            $row.Targets = $target.count

                            $row.Devices = $luns.count

                            $row.Paths = $nrpaths

                        } elseif ($row.vmhba -eq $null) {

                            $row.vmhba = $hba.Device

                            $row.Targets = $target.count

                            $row.Devices = $luns.count

                            $row.Paths = $nrpaths

                        }

                        $report += $row

                    }

                }

            }

        }

    }

    $report | Export-Csv -Path $outputFile -NoTypeInformation -UseCulture