VMware PowerCLI – Invoke-VMScript

So what happens after we create our VM’s?  Well typically we need to DO something with them which means we need to perhaps configure some disks, set IP address information, etc.

When I fist started my VMWare journey and was blissfully unaware of PowerCLI I would create a vm and then attach to the console to make all of these changes manually.

Thanks to our friends at VMWare I no longer have to do this.  It can all be handled using the “Invoke-VMScript” command through PowerCLI.  This lovely little function works with the vmware tools installation inside the VM to run commands for you.  (You do have vmware tools installed on all your templates correct?).

One example here is from a script I wrote to clone one of my applications to a test environment.  Keep in mind these are linux commands but you can also run commands against windows machines as well.

This snippet is run as part of a larger powercli script.  The script clones my application server so I can run tests against it such as upgrades or manipulating the postgres database directly before implementing on the production application server.  However, simply cloning the vm would not suffice as it would have the same host name and the same network configuration.  Using the Invoke-VMScript I am able to include these required changes into my clone script so that the vm gets cloned as well as re-named and re-ip’d.  Keep in mind this is just one example but pretty much anything you could manually run at a command prompt can be pushed through the Invoke-VMScript command.

Happy scripting!


## Get root OS credentials
$gc = Get-Credential

## Remove old MAC address
Invoke-VMScript -VM us-db-001-clone -ScriptText "sed -i '/HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth0" -GuestCredential $gc | out-null

## Set new IP address
Invoke-VMScript -VM us-db-001-clone -ScriptText "sed -i 's/192.168.10.29/192.168.23.29/g' /etc/sysconfig/network-scripts/ifcfg-eth0" -GuestCredential $gc | out-null
Invoke-VMScript -VM us-db-001-clone -ScriptText "sed -i 's/192.168.10.1/192.168.23.254/g' /etc/sysconfig/network" -GuestCredential $gc | out-null

## Set new Host Name
Invoke-VMScript -VM us-db-001-clone -ScriptText "sed -i 's/us-db-001/us-db-001-clone/g' /etc/sysconfig/network" -GuestCredential $gc | out-null
Invoke-VMScript -VM us-db-001-clone -ScriptText "sed -i 's/us-db-001/us-db-001-clone/g' /etc/hosts" -GuestCredential $gc | out-null
Invoke-VMScript -VM us-db-001-clone -ScriptText "sed -i 's/us-db-001/us-db-001-clone/g' /etc/hostname" -GuestCredential $gc | out-null

## Update Hosts file
Invoke-VMScript -VM us-db-001-clone -ScriptText "sed -i 's/192.168.10.29/192.168.23.29/g' /etc/hosts" -GuestCredential $gc | out-null

## Update Postgres config
Invoke-VMScript -VM us-db-001-clone -ScriptText "sed -i 's/192.168.10/192.168.23/g' /var/lib/pgsql/9.6/data/pg_hba.conf" -GuestCredential $gc | out-null

## Set new MAC address
Invoke-VMScript -VM us-db-001-clone -ScriptText "./changehwaddr.sh" -GuestCredential $gc | out-null

## Reboot for good measure
Invoke-VMScript -VM us-db-001-clone -ScriptText "reboot" -ErrorAction SilentlyContinue -GuestCredential $gc | out-null

Leave a Reply

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