PowerCLI – How to execute script remotely on ESXi hosts


I was recently helping a friend of mine who wanted to execute a single command on thousands of ESXi hosts and was looking for an easy way to do this. Enter PowerCLI, which is why this post is titled PowerCLI -- How to execute script remotely on ESXi hosts.

At first, I did a quick google search and it led to the community forum https://communities.vmware.com/thread/558507, but there were slight changes that were required for this to work.

We will be making use plink software, it is a command-line connection tool similar to UNIX ssh. It is mostly used for automated operations like scripting, etc.

The latest version can be downloaded from http://the.earth.li/~sgtatham/putty/0.63/x86/

So without further ado, here is the script that I wrote and executed on a single ESXi host, the same logic to any number of hosts that you would want to run this on.

Let me give you a brief understanding of what the script does.

We are first setting up some variables for the user account and password and the command that we need to execute remotely.

In my case, as a proof of concept, I am just listing the volumes available on the ESXi host.

$esxlist only has one host, but you can easily use a .csv and import many more hosts and run the foreach loop shown.

We have some error handling in the foreach loop, where it will enable the SSH service on the host it isn’t already.

The final step is to execute script remotely on ESXi hosts and save the output in a variable called $message and echo the same on the screen.

I hope this has been informative and thank you for reading!


About Author

I am Adil Arif, working as a Senior Technical Support Engineer at Rubrik as well as an independent blogger and founder of Enterprise Daddy. In my current role, I am supporting infrastructure related to Windows and VMware datacenters.

Leave A Reply