gopinathan
Contributor
Contributor

powercli script to get targets devices paths from HBA

Jump to solution

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?

1 Solution

Accepted Solutions
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

31 Replies
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

RvdNieuwendijk
Leadership
Leadership

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
gopinathan
Contributor
Contributor

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

0 Kudos
gopinathan
Contributor
Contributor

Thank you Robert.

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

0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

RvdNieuwendijk
Leadership
Leadership

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. Smiley Wink

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

gopinathan
Contributor
Contributor

You guys rocks. Love the community.

Happy Holidays!

0 Kudos
touimet
Enthusiast
Enthusiast

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!

0 Kudos
RvdNieuwendijk
Leadership
Leadership

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
touimet
Enthusiast
Enthusiast

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!

0 Kudos
lkovalio
Contributor
Contributor

hi LucD & Rob

any updates if i run it on ESXi 5??

0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
lkovalio
Contributor
Contributor

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?

0 Kudos
lkovalio
Contributor
Contributor

small update

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

only devices


0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
lkovalio
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

That's the latest version.

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
lkovalio
Contributor
Contributor

there is any way to pass it?

0 Kudos