Invoke-VMScript 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 Invoke-VMScript, 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 via Invoke-VMScript 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:

[email protected]
rm -rf /customizing.sh
touch /customizing.sh
echo “#!/bin/bash” >> /customizing.sh
echo “sed -i ‘1d’ /etc/resolv.conf” >> /customizing.sh
echo “sed -i ‘3d’ /etc/hosts” >> /customizing.sh
echo “sed -i ’12d’ /etc/sysconfig/network-scripts/ifcfg-eth0” >> /customizing.sh
echo “sed -i ’13d’ /etc/sysconfig/network-scripts/ifcfg-eth0” >> /customizing.sh
echo “sed -i ’14d’ /etc/sysconfig/network-scripts/ifcfg-eth0” >> /customizing.sh
echo “sed -i ’15d’ /etc/sysconfig/network-scripts/ifcfg-eth0” >> /customizing.sh
echo “sed -i ‘/^DOMAIN/d’ /etc/sysconfig/network-scripts/ifcfg-eth0” >> /customizing.sh
echo “sed -i ‘/^PEER/d’ /etc/sysconfig/network-scripts/ifcfg-eth0” >> /customizing.sh
echo “sed -i ‘s/static/none/g’ /etc/sysconfig/network-scripts/ifcfg-eth0” >> /customizing.sh
chmod +x /customizing.sh
‘@

After writing script as a file in guest OS, it can be run via Invoke-VMScript 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

Davoud Teimouri

Davoud Teimouri is as a professional blogger, vExpert 2015/2016/2017/2018, VCA, MCITP. This blog is started with simple posts and now, it has large following readers.

Leave a Reply

Your email address will not be published. Required fields are marked *