PowerCLI – Globally setting VM Tools time sync and version upgrade options

Recently I was presented with the need to change the time synchronization and VM Tools installation options for a number of VM guest machines.

While this is not challenging to do through the UI, I’m a big fan of doing everything possible through PowerCLI. And if you have larger numbers of machines in your VMware environment, those few clicks seem to get exponentially more difficult. Besides the convenience in greater numbers, it’s just good to become more familiar with PowerCLI wherevery possible.

Here is what we are looking at as the “before” view:

 

Even more exciting for my particular configuration is that I’ve got two vCenter servers running in Linked Mode. So this will add one requirement to the script to be able to connect to multiple instances of vCenter to enumerate the VM guests.

I am assuming that you are running your PowerCLI console with administrative privileges, otherwise you will be prompted for your credentials upon connecting to the VI servers.

If you only have one vCenter server, you can simply make the $VIServers = “yourservername” without a second server listed. Conversely, you may have more than two vCenter instances to speak to, or you may be connecting directly to the vSphere hosts. In those cases you may add additional server names in a comma separated list where the servername is enclosed in double quotes.

Here is the script:      

# Connect to your VI Server(s)
#
# Fill in the VI Server(s) using “servername”,”servername” depending on how many you wish to attach to
$VIServers = “vCenterServer1″,”vCenterServer2”

# Connect to the VI servers to enumerate the resources
ForEach ($VIServer in $VIServers) {
      Connect-VIServer $VIServer
}

# Query for the VM guests
$VMGuests = Get-VM

# Loop through your VM guests, set the VM Tools upgrade checkbox and the Sync Time checkbox to true
ForEach ($VMGuest in $VMGuests) {
      $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
      $spec.changeVersion = $VMGuest.ExtensionData.Config.ChangeVersion
      $spec.tools = New-Object VMWare.Vim.ToolsConfigInfo
      $spec.tools.toolsUpgradePolicy = “upgradeAtPowerCycle”
      $spec.tools.syncTimeWithHost = $true

      # Apply the changes
      $MyVM = Get-View -Id $VMGuest.Id
      $MyVM.ReconfigVM_Task($spec)
}

Once we have run the script, you will see activity in two places. Firstly you will see in your PowerCLI console that there are vCenter tasks being created and started:

Next you will be able to view inside your VMware vCenter client that a Reconfigure Virtual Machine task is submitted and after a very short time will show as completed.

Now that the process has completed, you may now check the VM settings in your VI Client and as if by magic, the checkboxes are both set!

It’s just that easy! In fact, this does not need to be a global change. I’ve only made it that way because of my specific situation. If you modify the $VMGuest = Get-VM to add parameters you could narrow the focus to some specific targets. In fact you will want to do this to test out most likely.

Here is a simple way to apply the changes to all servers whose name begins with WINSRV using the -Name parameter.

$VMGuest = Get-VM -Name WINSRV*

Also, if you want to make the change in the other direction to modify the VM Tools options to be unchecked, this is the line that you would use within the ForEach loop:

$spec.tools.toolsUpgradePolicy = “manual”

And for setting the Time Synchronization option to be unchecked, this is the line you would use:

$spec.syncTimeWithHost = $false

And that wraps it up. Enjoy!

8 thoughts on “PowerCLI – Globally setting VM Tools time sync and version upgrade options”

  1. This is a great script that saved me some dev time. Just what I needed as we are going to change all of our servers over to a different time sync setup.

    Just a side note of one little glitch I ran into (maybe it’s my env). I had to change from double quotes around the vCenter server names to single quotes – then it was happy! Still an awesome job though!

    Reply
  2. Hi Eric,
    I would like to propose a little modification to your script. Here is what happened to me: on a vCenter with a few thousands VMs I found duplicate VM IDs, which ran the script into trouble. I was surprised to see that, even if I specified clearly a set of VMs, other VMs were targeted too. So I changed the line
    $MyVM = Get-View -Id $VMGuest.Id
    into
    $MyVM = $VM | Get-View
    This way I eliminated the confusion.

    Regards

    Reply

Leave a Comment

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