VMware Cloud Community
mikefoley
VMware Employee
VMware Employee
Jump to solution

Set custom VMware tools scripts from PowerCLI?

Hi,

I'm wondering if it's possible, from PowerCLI, to set custom scripts in VMware Tools. These are the scripts that run when you suspend/resume/etc..

I looked in the docs and online and here and couldn't find anything. Do you need to use the VIX api for this?

thanks,

mike

mike

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There are 2 options:

1) you re-use the existing BAT files on the guest

2) you specify a new BAT file

For the 2nd option you will, as William pointed out, require the VIX APIs.

But for the first method you can do it with the current PowerCLI build

This script changes the content of the default poweroff BAT file.

 
$vmName = <vm-name>
$powerOffBatName = "poweroff-vm-default.bat"
$getProgPath = "echo %ProgramFiles%"

$newPowerOffBat = @"
Rem New PowerOff script
dir
"@

# Store new bat file locally
Set-Content -Path ($env:temp + "\" + $powerOffBat) -Force -Value $newPowerOffBat

$vmImpl = Get-VM $vmName
$vm = $vmImpl | Get-View

# rem Get credentials
$guestCred = Get-Credential -Credential "guestuser"
$hostCred = Get-Credential -Credential "hostuser"

# Get the content of %ProgramFiles% on the guest
$progPath = (Invoke-VMScript -GuestCredential $guestCred -HostCredential $hostCred `
	-VM $vmImpl `
	-ScriptText $getProgPath -ScriptType "Bat").Trim()

# Set the path of the file
$powerOffBatPath = $progPath + "\VMware\VMware Tools\" 

# Remove the read-only attribute
$unsetRO = 'attrib -R "' + $powerOffBatPath + $powerOffBat + '"'
Invoke-VMScript -GuestCredential $guestCred -HostCredential $hostCred `
	-VM $vmImpl `
	-ScriptText $unsetRO -ScriptType "Bat"
	
# Copy the new file
$destFile = $powerOffBatPath + $powerOffBat
Copy-VMGuestFile -Source ($env:temp + "\" + $powerOffBat) `
	-Destination $destFile `
	-GuestCredential $guestCred `
	-HostCredential $hostCred `
	-LocalToGuest -VM $vmImpl

# Set the read-only attribute
$setRO = 'attrib +R "' + $powerOffBatPath + $powerOffBat + '"'
Invoke-VMScript -GuestCredential $guestCred -HostCredential $hostCred `
	-VM $vmImpl `
	-ScriptText $setRO -ScriptType "Bat"
	
# Activate the option to run a script
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.tools = New-Object VMware.Vim.ToolsConfigInfo
$spec.tools.beforeGuestShutdown = $true
$vm.ReconfigVM_Task($spec)

Note that these default BAT files all are set to read-only, so the script changes that attributes and sets it back afterwards.

To make the script work on different Windows flavours, it gets the path from the %programfiles% environment variable on the guest.

And to make sure, the script also sets the Run VMware Tools script option for the poweroff action.

It should be rather straightforward to change this to any of the other options like suspend, reboot, resume...

Let me know if it does somewhat what you were looking for ?

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
lamw
Community Manager
Community Manager
Jump to solution

VIX is definitely required for this and there are some new PowerCLI cmdlets that contain VIX integration which allows you to transfer files to/from the guestVMs. What you would probably do is just upload your "version" of the power on/off/suspend scripts to the appropriate directory in which VMware Tools installs them by default and that should work. I'm sure the PowerCLI masters can give you the cmdlets + syntax to do so but this is something I've not tried before to replace the default scripts with your own. You may also want to think about it when you upgrade VMware Tools if the scripts get overridden

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Community

If you find this information useful, please award points for "correct" or "helpful".

RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can use the PowerCLI Copy-VMGuestFile cmdlet to copy files to a VM via the VMware tools. Something like:

$vm = Get-VM -Name myVM
Copy-VMGuestFile -Source c:\poweron-vm-default.bat -Destination "C:\Program Files\VMware\VMware Tools\" -VM $vm -LocalToGuest -HostUser root -HostPassword pass1 -GuestUser administrator -GuestPassword pass2

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
LucD
Leadership
Leadership
Jump to solution

There are 2 options:

1) you re-use the existing BAT files on the guest

2) you specify a new BAT file

For the 2nd option you will, as William pointed out, require the VIX APIs.

But for the first method you can do it with the current PowerCLI build

This script changes the content of the default poweroff BAT file.

 
$vmName = <vm-name>
$powerOffBatName = "poweroff-vm-default.bat"
$getProgPath = "echo %ProgramFiles%"

$newPowerOffBat = @"
Rem New PowerOff script
dir
"@

# Store new bat file locally
Set-Content -Path ($env:temp + "\" + $powerOffBat) -Force -Value $newPowerOffBat

$vmImpl = Get-VM $vmName
$vm = $vmImpl | Get-View

# rem Get credentials
$guestCred = Get-Credential -Credential "guestuser"
$hostCred = Get-Credential -Credential "hostuser"

# Get the content of %ProgramFiles% on the guest
$progPath = (Invoke-VMScript -GuestCredential $guestCred -HostCredential $hostCred `
	-VM $vmImpl `
	-ScriptText $getProgPath -ScriptType "Bat").Trim()

# Set the path of the file
$powerOffBatPath = $progPath + "\VMware\VMware Tools\" 

# Remove the read-only attribute
$unsetRO = 'attrib -R "' + $powerOffBatPath + $powerOffBat + '"'
Invoke-VMScript -GuestCredential $guestCred -HostCredential $hostCred `
	-VM $vmImpl `
	-ScriptText $unsetRO -ScriptType "Bat"
	
# Copy the new file
$destFile = $powerOffBatPath + $powerOffBat
Copy-VMGuestFile -Source ($env:temp + "\" + $powerOffBat) `
	-Destination $destFile `
	-GuestCredential $guestCred `
	-HostCredential $hostCred `
	-LocalToGuest -VM $vmImpl

# Set the read-only attribute
$setRO = 'attrib +R "' + $powerOffBatPath + $powerOffBat + '"'
Invoke-VMScript -GuestCredential $guestCred -HostCredential $hostCred `
	-VM $vmImpl `
	-ScriptText $setRO -ScriptType "Bat"
	
# Activate the option to run a script
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.tools = New-Object VMware.Vim.ToolsConfigInfo
$spec.tools.beforeGuestShutdown = $true
$vm.ReconfigVM_Task($spec)

Note that these default BAT files all are set to read-only, so the script changes that attributes and sets it back afterwards.

To make the script work on different Windows flavours, it gets the path from the %programfiles% environment variable on the guest.

And to make sure, the script also sets the Run VMware Tools script option for the poweroff action.

It should be rather straightforward to change this to any of the other options like suspend, reboot, resume...

Let me know if it does somewhat what you were looking for ?

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Mike, for more detailed annotations see my Changing VMware Tools scripts post.

Thanks for the idea Smiley Wink

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos