VMware Cloud Community
1NetPro
Contributor
Contributor
Jump to solution

Limiting a script to a list of hosts in a .txt file, instead of all hosts in the vCenter

Greetings all!

Found a jewel of a script to check for HBA path status that gives me exactly what I need, except it does it for all the hosts in the vCenter and I am having trouble making it work against a list of host names in text file.

For the record, this was posted by a Kath @ https://practical-admin.com/blog/powercli-show-hba-path-status/ as a response to a similar script that  wasn't working well. Thank you kindly!

Now, I have tested other scripts also written to query all hosts and was able to modify them to work with a host list by using:

$list = @(Get-Content "path-to-hostlist.txt")

$export = @()

foreach ($Item in $list) {

    $vmhost = get-vmhost | where Name -match $Item

    if ($vmhost) {

Or

$exportData = @()

$esxs=gc path-to-hostlist.txt

foreach($esxi in $esxs){

$esx = Get-VMHost -Name $esxi

But the script below is kicking my you-know-what with gusto and I can only get it work partially as some functions break after my changes.

Looking for help from someone with the know-how to make the tweak.

Many, many thanks!

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

#connect to vIServers

if ($global:DefaultVIServers.Count -ne 2) {

Connect-vIServer -Server server01.my.domain

Connect-vIServer -Server server02.my.domain

}

else {

Write-Host "Already connected to" $global:DefaultVIServers -ForegroundColor Green

}

#get all views from all datacentres

$views = @()

foreach ($server in $global:DefaultVIServers) {

$server.Name

$views += Get-View -ViewType "HostSystem" -Property Name, Config.StorageDevice -Server $server.name

}

$views = $views | sort name

#build array and export to clipboard

$index = 0

$array = @()

foreach ($view in $views) {

$index ++

write-host "Processing" $view.name $index "of" $views.Count -ForegroundColor Yellow

$hbas = $view.Config.StorageDevice.ScsiTopology.Adapter |?{ $_.Adapter -like "*FibreChannelHba*" }

write-host `t "Found" $hbas.count "HBAs" -ForegroundColor DarkYellow

foreach ($adapter in $hbas) {

$hba = $null

$hba = $adapter.adapter.split("-")[2]

$state = $null

$state = @()

$table = "" | select ESXHostName, Cluster, HBA_Adapter, HBA_Status, HBA_TargetPathCount, ActiveCount, DeadCount, StandbyCount

$table.ESXHostName = $View.Name

$table.HBA_Adapter = $hba

foreach ($target in $adapter.Target) {

write-host `t`t "Target:" $target.Target -ForegroundColor Magenta

foreach ($lun in $target.Lun) {

#write-host `t `t `t $lun.Key -ForegroundColor DarkMagenta

$id = $lun.ScsiLun.Split("-")[2]

$multipathInfo = $view.Config.StorageDevice.MultipathInfo.Lun | Where-Object { $_.id -eq $id }

$state += $multipathInfo.Path | Where-Object {$_.adapter -like "*$hba*"}

}

}

$grouped = $state | Group-Object state

$table.Cluster = (get-cluster -VMHost $view.Name).name

$table.HBA_Status = (Get-VMHostHba -VMHost $view.Name -Device $hba).Status

$table.HBA_TargetPathCount = $adapter.Target.Count

$table.ActiveCount = ($grouped| Where-Object {$_.name -like "*active*"}).count

$table.DeadCount = ($grouped| Where-Object {$_.name -like "*dead*"}).count

$table.StandbyCount = ($grouped| Where-Object {$_.name -like "*stand*"}).count

$array += $table

}

}

$array | ft -AutoSize

$array | ConvertTo-Csv -Delimiter "`t" -NoTypeInformation | clip.exe

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The script is using the Get-View cmdlet to retrieve the HostSystem objects.

On the Get-View cmdlet there is a Filter parameter, which accepts a RegEx string as the right-hand side operator.

You could change the Get-View line to

$filter = (Get-Content -path .\names.txt) -join '|'

$views = Get-View -ViewType "HostSystem" -Property Name,Config.StorageDevice -filter @{Name=$filter}

This assumes that the file contains just the names of the ESXi nodes, one per line.


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

The script is using the Get-View cmdlet to retrieve the HostSystem objects.

On the Get-View cmdlet there is a Filter parameter, which accepts a RegEx string as the right-hand side operator.

You could change the Get-View line to

$filter = (Get-Content -path .\names.txt) -join '|'

$views = Get-View -ViewType "HostSystem" -Property Name,Config.StorageDevice -filter @{Name=$filter}

This assumes that the file contains just the names of the ESXi nodes, one per line.


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

0 Kudos
1NetPro
Contributor
Contributor
Jump to solution

Once again Luc, your magic works wonderfully!

Results are perfect after the change you suggested.

I thank you very, very much!

0 Kudos