VMware Cloud Community
krismortensen
Contributor
Contributor

get targets, devices, and paths per hba with powercli

I have copied a powercli script from here: https://communities.vmware.com/thread/293531 and made a few minor changes.

The script is supposed to get all esxi hosts in a cluster, get all of the fiber hbas on each host, and check for the number of targets, devices, and paths on each of those hbas.

I have two problems with my script; the first is that when I run the script, it is reporting back 0 targets and 0 paths for each hba, which is clearly not accurate according to vcenter. How do I fix that? 

The second problem is that I want predefined thresholds for targets, devices, and paths; that way when the script runs, if a particular hba has different counts than what was set for the thresholds, I can get an alert sent to me. I don't have that written into the script, and don't know how. Can someone help?

The current script is as follows:

# Add the powercli snap in

Add-PSSnapin VMware.VimAutomation.Core

Set variables for later use

$viserver = "vcenterserver"

$cluster = "ProductionCluster"

# Connect to vcenter server

Try

         {

             "Attempting to connect to $viserver..."

             Connect-VIServer $viserver

         }

Catch [system.exception]

         {

            Write-Host "The script could not connect to $viserver. The error is: "

            "System Exception: $Error"

            Write-Host "The script will now exit."

            Remove-PSSnapin Vmware.VimAutomation.Core

            Exit

         }

# Count targets, devices, and paths for each host

Get-Cluster $cluster | Get-VMHost | Sort-Object -Property Name | ForEach-Object {

    $VMHost = $_

    $VMHost | Get-VMHostHba -Type FibreChannel | 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

    }

}

# Disconnect from vcenter

Disconnect-VIServer -Confirm:$false

# Remove the powercli snapin

Remove-PSSnapin Vmware.VimAutomation.Core

0 Kudos
0 Replies