4 Replies Latest reply on May 7, 2010 2:30 AM by LucD

    Set custom VMware tools scripts from PowerCLI?

    mikefoley Expert
    VMware EmployeesvExpert





      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?


















        • 1. Re: Set custom VMware tools scripts from PowerCLI?
          lamw Guru
          VMware EmployeesCommunity Warriors

          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: http://engineering.ucsb.edu/~duonglt/vmware/

          Twitter: @lamw

          vGhetto Script Repository

          Getting Started with the vMA (tips/tricks)

          Getting Started with the vSphere SDK for Perl

          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".

          1 person found this helpful
          • 2. Re: Set custom VMware tools scripts from PowerCLI?
            RvdNieuwendijk Virtuoso
            User ModeratorsvExpert

            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

            1 person found this helpful
            • 3. Re: Set custom VMware tools scripts from PowerCLI?
              LucD Guru
              vExpertCommunity WarriorsUser Moderators

              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
              # 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


              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

              • 4. Re: Set custom VMware tools scripts from PowerCLI?
                LucD Guru
                vExpertCommunity WarriorsUser Moderators

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


                Thanks for the idea






                Blog: LucD notes

                Twitter: lucd22