Changing VMware Tools scripts

There was another interesting question on the PowerCLI community. Mike was wondering if it would be possible to change the VMware Tools scripts with a PowerCLI script. As far as I can see there are two options available with the VMware Tools scripts.

  1. You can run the default scripts that are installed on the guests when you install VMware Tools
  2. You can use Custom scripts and you have to specify the path of these scripts to VMware Tools

To use the 2nd option you will need, as William already pointed out, the VIX APIs.

But if you can live with the first option, and reuse the default scripts, the current PowerCLI build has all you need.

The script

Annotations

Line 2: The name of the default script you want to change. See the table below for the names of all the available scripts

Line 3: The command that will get us the content of the %ProgramFiles% environment variable on the guest.

Line 5-8: The new content of the default script. This is where you enter your code you want to execute on the guest when one of the supported events takes place.

Line 11: The new version of the BAT file is stored locally in the %temp% folder.

Line 17-18: The script gathers the credentials for the guest and the host where the guest is hosted.

Line 21-23: Get the content of the %ProgramFiles% environment variable from the guest. Note that you need to use the Trim method to get rid off the <CR><LF> at the end of the returned string. Without parameters the Trim method removes all leading and trailing whitespace.

Line 29-32: Remove the read-only attribute from the file with the DOS attrib command.

Line 35-40: The actual copy of the file.

Line 43-46: Set the file attribute back to read-only

Line 49-52: Reconfigure the guest to make sure that the option to run the script is selected.

The script above uses the PowerOff action and the corresponding script, but it is rather straightforward to change this to any of the other available script events.

To make it easier to convert to another event you can use the following table

Event Default script Property
Suspend suspend-vm-default.bat beforeGuestStandby
Resume resume-vm-default.bat afterResume
Shut Down poweroff-vm-default.bat beforeGuestShutdown
Power On poweron-vm-default.bat afterPowerOn

Leave a Reply

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

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.