Execute-VMHostCommand.ps1 - plink.exe Wrapper for ESXi Hosts
Wrapper PowerCLI script that uses plink.exe to execute commands via SSH against one or more ESXi hosts. Useful to perform mass configuration changes using ESXi commands that PowerCLI does not expose, or to perform tasks such as restarting the management agents (/etc/init.d/hostd restart; /etc/init.d/vpxa restart.) The script is designed with the following in mind:
- Scales to one or more ESXi hosts from connected vCenter(s).
- Automatically starts the SSH service on an ESXi host, if not running.
- If the script started SSH on the ESXi host, it will also shutdown the service after completing execution of the command.
- Uses the PowerShell "PSCredential" object to capture/encrpt the ID/password used to authenticate against the ESXi host(s). Makes many best-effort measures to mask the password that is decrypted to pass into plink.exe (which accepts a password in plaintext.)
- Ability to enable/disable the plink.exe verbose switch.
- Ability to enable a switch to auto-accept the ESXi host's SSH key.
- Performs some parsing and cleanup of the output and error/verbose output from a single string into an array of strings.
- Echos back the command text, user ID, and date/time of the execution against each ESXi host to enable saving the output to a report / file (e.g. CSV.)
This script makes the following assumptions:
- You have a copy of plink.exe. You can download a copy from the Putty site. The script has a parameter to specify a location for the plink.exe location (default is C:\Putty\plink.exe.)
- You are connected to one or more vCenters via PowerCLI prior to executing the script.
- You're using a supported combination of PowerCLI, PowerShell, Windows and targeting a compatible vCenter.
- Multiple versions of PowerCLI, PowerShell and by extension Windows + vCenter are supported by this script. Testing was done against a vSphere 5.5 + PowerCLI 5.8R1 and vSphere 6.0 + PowerCLI 6.3R1.
For more information and examples with snapshots executing the script, please read my blog post: https://virtuallyunderstood.com/2016/04/30/powercli-script-for-executing-plink-exe-against-esxi-hosts/