PowerCLI: Best Way to Run Invoke-VMScript and Challenges with Linux Guest
PowerCLI is powerful automation tool for vSphere because administrator can do anything by that. Invoke-VMScript is one of most popular commands in PowerCLI.
Invoke-VMScript runs a script in the guest OS of each of the specified virtual machines. To run that, the user must have read access to the folder containing the virtual machine and a Virtual Machine.Interaction.Console Interaction privilege.
The virtual machines must be powered on and have VMware Tools installed. Network connectivity to the ESX/ESXi system hosting the virtual machine on port 902 must be present. To authenticate with the host or the guest OS, one of the HostUser/HostPassword (GuestUser/GuestPassword) pair and HostCredential (GuestCredential) parameters must be provided. The guest account you use to authenticate with the guest operating system must have administrator’s privileges.
Challenges with Linux Guest
There is some challenges for passing Linux commands with parameters to guest OS. In most of cases, administrators have to pass commands as a PowerShell variable also I had some bad experiences, as an example: run “Sed” with some special parameters. I have to use lot of double quotes and single quotes to pass command to guest OS.
Also may be the command will not run in guest OS properly and it can be cause of bad issues.
How to Run Commands Smoothly?
Why we run commands by Invoke-VMScript directly? Think about that. Instead of run commands, the commands can be write as bash script in guest OS.
Here an example, I went change some configurations on all Linux virtual machines. I’ve prepared the below script and run it by Invoke-VMScript:
After writing script as a file in guest OS, it can be run easily.
Another way for larger scripts, use “Copy-VMGuestFile” cmdlet to copy file between local and guest. The script can be uploaded to virtual machine directly and run the file by Invoke-VMScript.
Further Reading
[Script]: Check Time Synchronization with Host on Virtual Machines – PowerCLI
VMware Tools Client – Interact with a VM without Network Connectivity
[Script]: Enable/Disable vMotion on VMKernel Ports via PowerCLI
Run ESXi Commands Via PowerShell And SSH