VMware Cloud Community
mdthahir
Contributor
Contributor

Execute script over some subset of hosts in the cluster

I have a script that checks for, removes old and installs new VIBs on the hosts in my cluster. Problem is I currently only have it working on one host at a time through a foreach loop. I'd like to make the script execute across more hosts in the cluster parallelly, so essentially a subset of it. Any idea how I would be able to achieve that.

Code snippet:

# Enter vCenter Server to connect to

$server = Read-Host 'Enter vCenter Server FQDN'

Connect-VIServer -Server $server

# Get ESXi hostnames

$esxiServers = @(Get-Cluster | Get-VMHost | Sort-Object -Property Name)

foreach ($esxiServer in $esxiServers) {

$Endloop=$false

# Check if ESXi is in mainternance mode, if not set it in maintenance mode

$esxi = Get-VMHost -Name $esxiServer

Write-Host

Write-Host

Write-Host (Get-Date) ": Checking if $esxiServer is in maintenance mode" -ForegroundColor Yellow

Write-Host (Get-Date) ": $esxiServer is in "(Get-VMHost $esxiServer).ConnectionState' mode' -ForegroundColor Red

if ($esxi.ConnectionState -ne 'Maintenance') {

  Write-Host (Get-Date) ": Setting $esxiServer to maintenance mode" -Foregroundcolor Yellow

  Get-VMHost $esxiServer | Set-VMHost -State Maintenance -Confirm:$False -RunAsync | Out-Null

  sleep 10

...

Reply
0 Kudos
1 Reply
LucD
Leadership
Leadership

You could use background jobs with the Start-Job cmdlet.

See my Running a background job post.


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

Reply
0 Kudos