VMware Cloud Community
BrianDGeorge
Enthusiast
Enthusiast
Jump to solution

ESXi Inventory Problem with HBA WWNs

I have been working on this script in order to run reports for consistency and inventory verification in my environment.  I have finally hit a wall when it comes to HBAs and WWNs.  I can get one liners like the following working like a champ :

Get-VMhost | Get-VMHostHBA -Type FibreChannel | where{$_.Device -eq 'vmhba0'} | Select VMHost,Device,@{N='HBA Node WWP';E={$wwp = "{0:X}" -f $_.PortWorldWideName; (0..7 | %{$wwp.Substring($_*2,2)}) -join ':'}} | Sort VMhost,Device

The problem happens when I try to put everything together like in the following script.  I have commented out the portion that is not working:


#Variables
$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.ToString('MM-dd-yyyy_hh-mm-ss')
$dir = "C:\Reports\"
$ValidPath = Test-Path $dir
if ($ValidPath -eq $True) {Write-Host "The Path Exists" -foregroundcolor Green} Else {New-Item $dir -type directory}
$DVpassdir = "C:\Scripts\admin-securestring.txt"
$SVpassdir = "C:\Scripts\BALVC01-admin-securestring.txt"
$ValidDVPass = Test-Path $DVpassdir
$ValidSVPass = Test-Path $SVpassdir
if ($ValidDVPass -eq $True) {Write-Host "The File and Path Exist!!" -foregroundcolor Green} Else {Write-Host "File Does Not Exist!!" -foregroundcolor Magenta}
if ($ValidSVPass -eq $True) {Write-Host "The File and Path Exist!!" -foregroundcolor Green} Else {Write-Host "File Does Not Exist!!" -foregroundcolor Magenta}

#Credential
$username = "administrator@vsphere.local"
$password = cat "C:\Scripts\admin-securestring.txt" | convertto-securestring
$SVpassword = cat "C:\Scripts\XX-admin-securestring.txt" | convertto-securestring
$DVcreds = new-object -typename System.Management.Automation.PSCredential `
         -argumentlist $username, $password
$SVcreds = new-object -typename System.Management.Automation.PSCredential `
         -argumentlist $username, $SVpassword

#Modules#
Import-Module VMware.PowerCLI
        
####Custom PowerShell Functions####
#Site Menu#
function Show-vCenterMenu
{
     param (
           [string]$Title = 'vCenter'
     )
     cls
     Write-Host "================ $Title ================"
     
     Write-Host "1: Press '1' for AB-VCENTER1."
     Write-Host "2: Press '2' for CD-VCENTER1."
     Write-Host "3: Press '3' for AB-VIEWVC01."
     Write-Host "4: Press '4' for CD-VIEWVC01."
     Write-Host "5: Press '5' for Non-View AB-CD vCenters."
     Write-Host "6: Press '6' for ALL AB-CD vCenters."
     Write-Host "7: Press '7' for EF-VCENTER1."
     Write-Host "8: Press '8' for All AB-CD-EF vCenters."
     Write-Host "Q: Press 'Q' to quit."
}

#Connection to Host
#Variables
$vCenter =
do
{
     Show-vCenterMenu
     $input = Read-Host "Please make a selection"
     switch ($input)
     {
           '1' {
                cls
                'AB-VCENTER1'
           } '2' {
                cls
                'CD-VCENTER1'
           } '3' {
                cls
                'AB-VIEWVC01'
           } '4' {
                cls
                'CD-VIEWVC01'
           } '5' {
                cls
                "AB-VCENTER1", "CD-VCENTER1"
           } '6' {
                cls
                "AB-VCENTER1", "CD-VCENTER1", "AB-VIEWVC01", "CD-VIEWVC01"
           } '7' {
                cls
                "EF-VCENTER1"
           } '8' {
                cls
                "AB-VCENTER1", "CD-VCENTER1", "AB-VIEWVC01", "CD-VIEWVC01", "EF-VCENTER1"
           } 'q' {
                return
           }
     }
}
until ($input -eq '1' -or '2' -or '3' -or '4' -or '5' -or '6' -or '7' -or '8' -or 'q')


#Connect to vCenter based on vCenter
foreach ($vc in $vCenter)
{
    if($vc -eq 'AB-VCENTER1'){
    Connect-VIServer "$vc" -Credential $DVcreds -WarningAction SilentlyContinue
    }elseif ($vc -eq 'CD-VCENTER1'){
    Connect-VIServer "$vc" -Credential $DVcreds -WarningAction SilentlyContinue
    }elseif ($vc -eq 'AB-VIEWVC01'){
    Connect-VIServer "$vc" -Credential $DVcreds -WarningAction SilentlyContinue
    }elseif ($vc -eq 'CD-VIEWVC01'){
    Connect-VIServer "$vc" -Credential $DVcreds -WarningAction SilentlyContinue
    }elseif ($vc -eq "AB-VCENTER1", "CD-VCENTER1"){
    Connect-VIServer "$vc" -Credential $DVcreds -WarningAction SilentlyContinue
    }elseif ($vc -eq "AB-VCENTER1", "CD-VCENTER1", "AB-VIEWVC01", "CD-VIEWVC01"){
    Connect-VIServer "$vc" -Credential $DVcreds -WarningAction SilentlyContinue
    }elseif ($vc -eq 'EF-VCENTER1'){
    Connect-VIServer "$vc" -Credential $SVcreds -WarningAction SilentlyContinue
    }else {
    Connect-VIServer "AB-VCENTER1", "CD-VCENTER1", "AB-VIEWVC01", "CD-VIEWVC01" -Credential $DVcreds -WarningAction SilentlyContinue;
    Connect-VIServer "EF-VCENTER1" -Credential $SVcreds -WarningAction SilentlyContinue
    }
}

get-vmhost |
Select Name,
    Parent,
    Manufacturer,
    Model,
    @{N="PhyMem"; E={[math]::truncate($_.MemoryTotalMB / 1024)}},
    @{N="Cores/CPU";E={$_.Extensiondata.Summary.Hardware.NumCpuCores/$_.Extensiondata.Summary.Hardware.NumCpuPkgs}},
    @{N="Sockets";E={$_.Extensiondata.Summary.Hardware.NumCpuPkgs}},
    @{N="CPU Details";E={($_| Get-View).Hardware.CpuPkg[0].Description}},
    #@{N="Product";E={(Get-View $_.ID).Config.Product.FullName }},
    Build,
    LicenseKey,
    @{N="BIOS version";E={($_| Get-View).Hardware.BiosInfo.BiosVersion}},
    @{N="BIOS date";E={($_| Get-View).Hardware.BiosInfo.releaseDate}},
    @{N="HBA Model";E={($_.ExtensionData.Config.StorageDevice.HostBusAdapter | Where {$_.Key -like "*FibreChannel*"})[0].Model}},
    @{N="HBA Model";E={($_.ExtensionData.Config.StorageDevice.HostBusAdapter | Where {$_.Key -like "*FibreChannel*"})[0].Driver}},
#    @{N="HBA vmhba0 WWN";E={(Get-VMHostHBA -Type FibreChannel | where{$_.Device -eq 'vmhba0'}) | Where {$wwp = "{0:X}" -f $_.PortWorldWideName; (0..7 | %{$wwp.Substring($_*2,2)}) -join ':'}}},
#    @{N="HBA vmhba1 WWN";E={(Get-VMHostHBA -Type FibreChannel | where{$_.Device -eq 'vmhba0'}) | Where {$wwp = "{0:X}" -f $_.PortWorldWideName; (0..7 | %{$wwp.Substring($_*2,2)}) -join ':'}}},
#    @{N="HBA vmhba2 WWN";E={(Get-VMHostHBA -Type FibreChannel | where{$_.Device -eq 'vmhba0'}) | Where {$wwp = "{0:X}" -f $_.PortWorldWideName; (0..7 | %{$wwp.Substring($_*2,2)}) -join ':'}}},
    @{N="Uptime"; E={New-Timespan -Start $_.ExtensionData.Summary.Runtime.BootTime -End (Get-Date) | Select -ExpandProperty Days}},
    @{N="NTPServer";E={(Get-VMHostNtpServer -VMHost $_) -join ','}},
    @{N="ServiceRunning";E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq "ntpd"}).Running}},
    @{N="Time";E={(get-view $_.ExtensionData.configManager.DateTimeSystem).QueryDateTime()}},
    @{N="NTP Policy";E={(Get-VmHostService -VMHost $_ | Where-Object {$_.key-eq "ntpd"} | Select -ExpandProperty Policy)}},
    @{N="DNS servers";E={(Get-View $_.Extensiondata.configManager.networkSystem).networkConfig.dnsConfig.address -join ','}},
    @{N="Management Network";E={(Get-VMHostNetworkAdapter -VMHost $_) | Where-Object {$_.DeviceName-eq "VMK0"} | select -ExpandProperty IP}},
    @{N="vMotion1 Network";E={(Get-VMHostNetworkAdapter -VMHost $_) | Where-Object {$_.DeviceName-eq "VMK1"} | select -ExpandProperty IP}},
    @{N="vMotion2 Network";E={(Get-VMHostNetworkAdapter -VMHost $_ | Where-Object {$_.DeviceName-eq "VMK2"} | select -ExpandProperty IP)}}    |
Sort Name |
Export-CSV "C:\Reports\ESXi_Host_Report_$CurrentDate-VH.csv"
Disconnect-VIServer -Server * -Force -WarningAction SilentlyContinue -Confirm:$false

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

get-vmhost |

Select Name,

  Parent,

  Manufacturer,

  Model,

   @{N="PhyMem"; E={[math]::truncate($_.MemoryTotalMB / 1024)}},

   @{N="Cores/CPU";E={$_.Extensiondata.Summary.Hardware.NumCpuCores/$_.Extensiondata.Summary.Hardware.NumCpuPkgs}},

   @{N="Sockets";E={$_.Extensiondata.Summary.Hardware.NumCpuPkgs}},

   @{N="CPU Details";E={($_| Get-View).Hardware.CpuPkg[0].Description}},

   @{N="Product";E={(Get-View $_.ID).Config.Product.FullName }},

  Build,

  LicenseKey,

   @{N="BIOS version";E={($_| Get-View).Hardware.BiosInfo.BiosVersion}},

   @{N="BIOS date";E={($_| Get-View).Hardware.BiosInfo.releaseDate}},

   @{N="HBA Model";E={($_.ExtensionData.Config.StorageDevice.HostBusAdapter | Where {$_.Key -like "*FibreChannel*"})[0].Model}},

   @{N="HBA Driver";E={($_.ExtensionData.Config.StorageDevice.HostBusAdapter | Where {$_.Key -like "*FibreChannel*"})[0].Driver}},

   @{N="HBA vmhba0 WWN";E={

   $hba = Get-VMHostHBA -Type FibreChannel | where{$_.Device -eq 'vmhba0'}

   $wwp = "{0:X}" -f $hba.PortWorldWideName

   (0..7 | %{$wwp.Substring($_*2,2)}) -join ':'

   }},

   @{N="HBA vmhba1 WWN";E={

   $hba = Get-VMHostHBA -Type FibreChannel | where{$_.Device -eq 'vmhba1'}

   $wwp = "{0:X}" -f $hba.PortWorldWideName

   (0..7 | %{$wwp.Substring($_*2,2)}) -join ':'

   }},

   @{N="HBA vmhba2 WWN";E={

   $hba = Get-VMHostHBA -Type FibreChannel | where{$_.Device -eq 'vmhba2'}

   $wwp = "{0:X}" -f $hba.PortWorldWideName

   (0..7 | %{$wwp.Substring($_*2,2)}) -join ':'

   }},

   @{N="Uptime"; E={New-Timespan -Start $_.ExtensionData.Summary.Runtime.BootTime -End (Get-Date) | Select -ExpandProperty Days}},

   @{N="NTPServer";E={(Get-VMHostNtpServer -VMHost $_) -join ','}},

   @{N="ServiceRunning";E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq "ntpd"}).Running}},

   @{N="Time";E={(get-view $_.ExtensionData.configManager.DateTimeSystem).QueryDateTime()}},

   @{N="NTP Policy";E={(Get-VmHostService -VMHost $_ | Where-Object {$_.key-eq "ntpd"} | Select -ExpandProperty Policy)}},

   @{N="DNS servers";E={(Get-View $_.Extensiondata.configManager.networkSystem).networkConfig.dnsConfig.address -join ','}},

   @{N="Management Network";E={(Get-VMHostNetworkAdapter -VMHost $_) | Where-Object {$_.DeviceName-eq "VMK0"} | select -ExpandProperty IP}},

   @{N="vMotion1 Network";E={(Get-VMHostNetworkAdapter -VMHost $_) | Where-Object {$_.DeviceName-eq "VMK1"} | select -ExpandProperty IP}},

   @{N="vMotion2 Network";E={(Get-VMHostNetworkAdapter -VMHost $_ | Where-Object {$_.DeviceName-eq "VMK2"} | select -ExpandProperty IP)}}   |

Sort Name

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

Was it helpful? Let us know by completing this short survey here.


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

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

get-vmhost |

Select Name,

  Parent,

  Manufacturer,

  Model,

   @{N="PhyMem"; E={[math]::truncate($_.MemoryTotalMB / 1024)}},

   @{N="Cores/CPU";E={$_.Extensiondata.Summary.Hardware.NumCpuCores/$_.Extensiondata.Summary.Hardware.NumCpuPkgs}},

   @{N="Sockets";E={$_.Extensiondata.Summary.Hardware.NumCpuPkgs}},

   @{N="CPU Details";E={($_| Get-View).Hardware.CpuPkg[0].Description}},

   @{N="Product";E={(Get-View $_.ID).Config.Product.FullName }},

  Build,

  LicenseKey,

   @{N="BIOS version";E={($_| Get-View).Hardware.BiosInfo.BiosVersion}},

   @{N="BIOS date";E={($_| Get-View).Hardware.BiosInfo.releaseDate}},

   @{N="HBA Model";E={($_.ExtensionData.Config.StorageDevice.HostBusAdapter | Where {$_.Key -like "*FibreChannel*"})[0].Model}},

   @{N="HBA Driver";E={($_.ExtensionData.Config.StorageDevice.HostBusAdapter | Where {$_.Key -like "*FibreChannel*"})[0].Driver}},

   @{N="HBA vmhba0 WWN";E={

   $hba = Get-VMHostHBA -Type FibreChannel | where{$_.Device -eq 'vmhba0'}

   $wwp = "{0:X}" -f $hba.PortWorldWideName

   (0..7 | %{$wwp.Substring($_*2,2)}) -join ':'

   }},

   @{N="HBA vmhba1 WWN";E={

   $hba = Get-VMHostHBA -Type FibreChannel | where{$_.Device -eq 'vmhba1'}

   $wwp = "{0:X}" -f $hba.PortWorldWideName

   (0..7 | %{$wwp.Substring($_*2,2)}) -join ':'

   }},

   @{N="HBA vmhba2 WWN";E={

   $hba = Get-VMHostHBA -Type FibreChannel | where{$_.Device -eq 'vmhba2'}

   $wwp = "{0:X}" -f $hba.PortWorldWideName

   (0..7 | %{$wwp.Substring($_*2,2)}) -join ':'

   }},

   @{N="Uptime"; E={New-Timespan -Start $_.ExtensionData.Summary.Runtime.BootTime -End (Get-Date) | Select -ExpandProperty Days}},

   @{N="NTPServer";E={(Get-VMHostNtpServer -VMHost $_) -join ','}},

   @{N="ServiceRunning";E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq "ntpd"}).Running}},

   @{N="Time";E={(get-view $_.ExtensionData.configManager.DateTimeSystem).QueryDateTime()}},

   @{N="NTP Policy";E={(Get-VmHostService -VMHost $_ | Where-Object {$_.key-eq "ntpd"} | Select -ExpandProperty Policy)}},

   @{N="DNS servers";E={(Get-View $_.Extensiondata.configManager.networkSystem).networkConfig.dnsConfig.address -join ','}},

   @{N="Management Network";E={(Get-VMHostNetworkAdapter -VMHost $_) | Where-Object {$_.DeviceName-eq "VMK0"} | select -ExpandProperty IP}},

   @{N="vMotion1 Network";E={(Get-VMHostNetworkAdapter -VMHost $_) | Where-Object {$_.DeviceName-eq "VMK1"} | select -ExpandProperty IP}},

   @{N="vMotion2 Network";E={(Get-VMHostNetworkAdapter -VMHost $_ | Where-Object {$_.DeviceName-eq "VMK2"} | select -ExpandProperty IP)}}   |

Sort Name

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

Was it helpful? Let us know by completing this short survey here.


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

0 Kudos
BrianDGeorge
Enthusiast
Enthusiast
Jump to solution

Absolutely amazing, wish I had a fraction of your PowerCli knowledge.  Thank you for always supporting the community, even on weekends when some of us can't stop racking their heads over a problem!.

0 Kudos
BrianDGeorge
Enthusiast
Enthusiast
Jump to solution

Had to make one small change as it got stuck in a loop. Changed the $wwp variable to be unique for each hba.

@{N="HBA vmhba0 WWN";E={$hba = Get-VMHostHBA -Type FibreChannel | where{$_.Device -eq 'vmhba0'}

     $wwp0 = "{0:X}" -f $hba.PortWorldWideName

     (0..7 | %{$wwp0.Substring($_*2,2)}) -join ':'

     }},

@{N="HBA vmhba1 WWN";E={$hba = Get-VMHostHBA -Type FibreChannel | where{$_.Device -eq 'vmhba1'}

     $wwp1 = "{0:X}" -f $hba.PortWorldWideName

     (0..7 | %{$wwp1.Substring($_*2,2)}) -join ':'

     }},

@{N="HBA vmhba2 WWN";E={$hba = Get-VMHostHBA -Type FibreChannel | where{$_.Device -eq 'vmhba2'}

     $wwp2 = "{0:X}" -f $hba.PortWorldWideName

     (0..7 | %{$wwp2.Substring($_*2,2)}) -join ':'

     }},

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Strange, normally that $wwp variable should only be known inside the code block for the calculated property expression.

It could be that you already had a $wwp variable defined in a higher scope.

Did you stop/start your PS session, before testing the new code?


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

0 Kudos
BrianDGeorge
Enthusiast
Enthusiast
Jump to solution

I have rerun it several times from clean PS sessions and I noticed that for some reason it is doing weird things with the WWNs.  Each server has 2 HBAs but it is filling 3, the only reason I have code for 3 is that some are named vmhba0, vmhba1, vmhba2.  This is what the output for a small run is giving.

    

HBA ModelHBA DriverHBA vmhba0 WWNHBA vmhba1 WWNHBA vmhba2 WWN
Emulex OneConnect OCe11100 FCoE Initiatorbrcmfcoe10:00:20:67:7C:5D:9E:0110:00:20:67:7C:5D:9E:0910:00:6C:3B:E5:BC:DA:65
Emulex OneConnect OCe11100 FCoE Initiatorbrcmfcoe10:00:20:67:7C:5D:9E:0110:00:20:67:7C:5D:9E:0910:00:6C:3B:E5:BC:DA:65
Emulex OneConnect OCe11100 FCoE Initiatorbrcmfcoe10:00:20:67:7C:5D:9E:0110:00:20:67:7C:5D:9E:0910:00:6C:3B:E5:BC:DA:65
Emulex OneConnect OCe11100 FCoE Initiatorbrcmfcoe10:00:20:67:7C:5D:9E:0110:00:20:67:7C:5D:9E:0910:00:6C:3B:E5:BC:DA:65
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you give it a try with the VMhost parameter added?
Like this

@{N="HBA vmhba0 WWN";E={

   $hba = Get-VMHostHBA -VMHost $_ -Type FibreChannel | where{$_.Device -eq 'vmhba0'}

   $wwp = "{0:X}" -f $hba.PortWorldWideName

   (0..7 | %{$wwp.Substring($_*2,2)}) -join ':'

   }},


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

0 Kudos