Administering ESXi Hosts With ESXCLI using PowerCLI

Posted by Stephan McTighe on 10 Feb 2021

There are times as a vSphere admin, you are going to want to run ESXCLI commands against multiple ESXi Hosts from a central location. This could be for configuration / administration, reporting, patching or a number of other things.

Recently I have been testing different values in the /DataMover/MaxHWTransferSize advanced setting. To make life easier, I wanted a way to change multiple hosts quickly and easily. To do this, I customised a script that Luc Dekens posted as a solution to a problem someone was having that can be used to send ESXCLI commands to multiple hosts using PowerCLI and plink.exe. This slightly modified version uses a CSV file as a source containing my hosts FQDN and the username and password I will be connecting with.

Plink, which is part of the PuTTy suite, can can be found here.

When using this script, you need to either run the script from a directory containing the plink executable, copy it to where you want to run the script, or adjust the script to include the path to the plink executable… whichever takes your fancy.

Disclaimer: Always complete your own testing in an appropriate environment and refer to the vendors official documentation!

 1$Hosts = Import-Csv C:\ESXiHosts.csv
 2$Commad = 'esxcfg-advcfg -s 16384 /DataMover/MaxHWTransferSize'
 4Foreach ($H in $Hosts) {
 5    #Starting the SSH Service if not already started
 6    $SSHService = Get-VMHostService -VMHost $H.HostName | where {$_.Key -eq 'TSM-SSH'}
 7    if ($SSHService.Running -eq 'True') {
 8        Write-Host "****************************" -ForegroundColor Blue
 9        Write-Host "WARNING: SSH already enabled, this will be stopped on completion of this script" -ForegroundColor Yellow
10    }
11        Else {
13            Write-Host "Starting SSH Service on Host $($H.HostName)" -ForegroundColor Green
14            Start-VMHostService -HostService $SSHService -Confirm:$false > $null
15        }
16    #Running the defined ESXCLI Command(s)
17    Write-host "Running remote SSH commands on $($H.HostName)." -ForegroundColor Green
18    Echo Y | ./plink.exe $H.HostName -pw $H.Password -l $H.UserName $Commad
20    #Stopping the SSH Service
21    $SSHService = Get-VMHostService -VMHost $H.HostName | where {$_.Key -eq 'TSM-SSH'}
22    if ($SSHService.Running) {
23        Write-Host "Stopping SSH Service on Host $($H.HostName)" -ForegroundColor Green
24        Stop-VMHostService -HostService $SSHService -Confirm:$false > $null
25        Write-Host "****************************" -ForegroundColor Blue
26    }
28Write-Host "Complete $(Get-Date)" -ForegroundColor Green

You can run as many commands as you need by declaring another ‘Command’ variable at the beginning of the script and adding another line to the ‘Running the defined ESXCLI Command(s)’ section.

When run, it will then cycle through each of the ESXi hosts from your CSV file, enable SSH (if its not already enabled), accept the host key, run the commands you have specified and finally turn the SSH service off.

Here you can see it has set the MaxHWTransferSize to 16384 on each host.

You will see the Recent Task pane show the SSH Service starts and stops.

The commands passed in can be anything you need. All you need to do is change the commands that are defined in the variables section. For example, restarting the management agents -

1$commad = 'etc/init.d/hostd restart'
2$commad2 = 'etc/init.d/vpxa restart'

I hope this has been of use for anyone needing a centralised, quick way to administer multiple hosts via ESXCLI.

Thanks for reading!