PowerShell to bulk remove VIBs from multiple ESXi hosts (putting hosts in MM mode beforehand)

During my last upgrade from vSphere 6.7 to vSphere 7.0 on one of my clusters, I come across the issue with VIBs. Of course, the upgrade process was stopped and I saw the error shown in the picture below:

As you can see the baseline is Incompatible and The upgrade has VIBs that are missing dependencies. The solution for this problem is to remove VIBs causing the issue.

NOTE: Test removing on one host and check if your hosts can boot up after VIBs were removed!

As we know the command for VIB removing is:

esxcli software vib remove -n <ViB Name>

However, if you have a number of VIBs and a number of hosts, doing this one by one is not feasible. Let’s think about how could we automate the process.

  • The first thing we need to grab hosts:
$hosts = Get-Cluster "Cluster Name" | Get-VMHost
  • Let’s create a loop to remove VIBs:
foreach ($host in $hosts) {
  # Remove the VIB from the host
  Remove-VIB -VibName <VIB name> -VMHost $host
}
  • Let the user know the job is done:
 # Print a message
  Write-Host "Removed VIB from host $($host.Name)"
}

There is an issue, however with this approach. The ESXi host needs to be rebooted. And to reboot the ESXi host, we need to put it first in maintenance mode. Which this script does not do. Again manually putting 100 or more hosts and rebooting each other is not fun at all.

OK, let’s come up with another script, which will get all the hosts from the cluster, put one of them in maintenance mode, remove VIBs, reboot the host, exit maintenance mode, check if the host is back if so, go to another host.

BTW, do not forget to connect to vCenter:

Connect-VIserver <vCenter server name or IP address>
  • Let’s grab the info about hosts, like before
$hosts = Get-Cluster "Cluster Name" | Get-VMHost
  • Now we create a loop to put hosts in MM mode:
foreach ($vihost in $hosts){

Set-VMHost -VMHost $vihost -State Maintenance -Confirm:$false
Write-Host -ForegroundColor Yellow "Waiting for host to go in to MM mode" 

sleep 100

NOTE: The value of sleep you need to modify accordingly to your environment.

This is the part of the code that caused me confusion and I had to do some research about it: CreateArgs().

The CreateArgs() method is used to create a collection of command-line arguments for a script. This method allows you to specify the arguments that should be passed to a script when it is run and returns an System.Collections.ObjectModel.Collection<T> object that contains the arguments.

The CreateArgs() method is part of the System.Management.Automation.PSParser class, which is used to parse PowerShell scripts and command-line arguments. You must import this class in order to use the CreateArgs() method in your script.

  • Let’s remove those stubborn VIBs:
$esxcli = Get-EsxCli -VMHost $vihost -V2
$esxcliRemoveVibArgs = $esxcli.software.vib.remove.CreateArgs()
$vibs = $esxcli.software.vib.list.Invoke() | where{$_.Name -match "dell-configuration" -or $_.Name -match "dellemc-osname-idrac" -or $_.Name -match "qedf"  -or $_.Name -match "qedentv-ens"  -or $_.Name -match "qedi" }

NOTE: dell-configuration, dellemc-osname-idrac, etc. are VIBs that I had to remove. Your VIBs could have different names, so please do not copy blandly.

foreach ($vib in $vibs){
	$esxcliRemoveVibArgs.vibname = $vib.Name 
	$esxcli.software.vib.remove.Invoke($esxcliRemoveVibArgs)
}
  • It’s time to reboot the host:
Restart-VMHost $vihost -Confirm:$false 
  • Now the time is for the magic to wait until the host is back and running, remember the sleep parameters need to be customized by your needs:
do {

    sleep 100 
    $HostState = (Get-VMHost $vihost).ConnectionState
    write-host -ForegroundColor Red -NoNewline "."
    }
    while ($HostState -ne "NotResponding")
    
 do {
        sleep 100
        $HostState = (Get-VMHost $vihost).ConnectionState
        
    } while ($HostState -ne "Maintenance")
    
    Write-Host -ForegroundColor Yellow "`tServer Exiting Maintenance Mode"
    Set-VMHost $vihost -State Connected  | Out-Null
    Write-Host -ForegroundColor Yellow "`tHost Reboot Cycle Done!"
    Write-Host ""

And that would be it. The full script can be found in my GitHub account.

🔥Subscribe to the channel: https://bit.ly/3vY16CT🔥

🚨Read my blog: https://angrysysops.com/

👊Twitter: https://twitter.com/AngrySysOps
👊Facebook: https://www.facebook.com/AngrySysOps
👊My Podcast: https://bit.ly/39fFnxm

🔥vExpert info: https://bit.ly/3vXGPOa

🛒 VMware EMEA store: https://imp.i263671.net/c/3505578/814646/11461

🛒 VMware US store: https://imp.i263671.net/c/3505578/814642/11461

🛒 VMware APAC store: https://imp.i263671.net/c/3505578/814645/11461

Please leave the comment