Skip to content
vJAL.nl
  • Partners
    • Vembu
  • About me
  • Home
  • Search Icon
Configuring a VMware Tools Repository leveraging PowerCLI (6.7U1 and newer)

Configuring a VMware Tools Repository leveraging PowerCLI (6.7U1 and newer)

12 August 2020 Jesper Alberts Comments 0 Comment

Heads up: this script only functions with vSphere 6.7 Update 1 and newer

Ever since version 10.0 VMware has decoupled VMware Tools from vSphere releases. This gives you the opportunity to deploy images of ESXi without VMware Tools integrated, which saves you disk space and speeds up the deployment process.

Another benefit of this change is that you can now standardize your environment based on a single version by configuring a centralized repository, which gives you option to be in control of which version your environment is running with minimal effort.

In early 2019 VMware published a blog post on how to do this manually through the Managed Object Browser (MOB). However doing so is quite the task at it involves a lot of clicking for just one host, now try and do this for 20 ESXi hosts.

To make life easier I decided to create a PowerCLI script which loops all ESXi hosts for a given vCenter Server (you could add filters to only alter ESXi hosts with a specific property) and changes the directory for the “ProductLockerLocation” for said ESXi hosts.

Let’s take a look at the PowerCLI script step by step.

Table of Contents

  • Step 1
  • Step 2
  • Step 3
  • Step 4

Step 1

The first section is about connecting to vCenter using the information you’ll be prompted for upon running the script. You can safely alter this to provide the credentials in the script, use New-VICredentialStoreItem or save the credentials in an encrypted file.

    # Ask the user for the required information to connect to vCenter
    $vCenterServer = Read-Host "Enter the vCenter Server hostname"
    $AdminUsername = Read-Host ("Enter the username for the administrative account")
    $AdminPassword = Read-Host ("Password for " + $AdminUsername) -AsSecureString:$true
    $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $AdminUsername, $AdminPassword

    # Connect to the specified vCenter Server
    Connect-viserver -Server $vCenterServer -Credential $Credentials

Step 2

The second section is about getting the information required to actually start changing something. In my case I tend to use Get-VMHost to fetch all ESXi hosts and alter the configuration, but you’re free to add some sort of filter here.
The second action that takes place is that you’ll be prompted with an overview with the available datastores in the environment, of which you’re required to select the one on which you’ll upload the VMware Tools files.
Third and finally in this step is that you’ll be prompted to provide a name for the folder which will be created (if it’s not yet available). This is the folder in which you’ll have to place the VMware Tools files.

    # Specify different variables used in the script
    $ESXiHosts = Get-VMHost
    $DSName = (Get-Datastore | Out-GridView -PassThru).Name
    $DSFolder = Read-Host ("Enter the folder name for the VMware Tools Repository")
    $DSPath = "/vmfs/volumes/$DSName"
    $Location = "$DSPath/$DSFolder"

Step 3

After the second section the script will mount a PSDrive and check if the folder already exists. If it doesn’t exist, it will be created with the information specified in the previous part.
If it happens to already exist, the script will move to changing the actual configuration.

    # Start with testing if the directory exists, if not it will be created. Otherwise the VMware Tools location will be changed.
    Write-Output "Checking if the folder already exists on the datastore"
    $Datastore = Get-Datastore -Name "$DSName"
    New-PSDrive -Location $Datastore -Name DS -PSProvider VimDatastore -Root "\" | Out-Null
    $CheckPath = Test-Path "DS:\$DSFolder"

    If ($CheckPath -match "False") {
      Write-Output "The folder '$DSFolder' on datastore '$DSName' does not exist, creating it now."
      # Creating the folder on the datastore
      New-Item -Path "DS:\$DSFolder" -ItemType Directory | Out-Null
      #  Disconnects the earlier created PSDrive
      Remove-PSDrive -Name DS -Confirm:$false | Out-Null
    }

Step 4

Once everything is prepared we can now start changing the configuration itself.

    ForEach ($ESXiHost in $ESXiHosts) {
      $esx = Get-VMHost -Name $ESXiHost
      $OldLocation = $esx.ExtensionData.QueryProductLockerLocation()
      $esx.ExtensionData.UpdateProductLockerLocation($Location) | Out-Null
      Write-Output "Changed VMware Tools location on $ESXiHost from $OldLocation to $Location"
    }

After which the script will disconnect from vCenter and calls it a day.
The script is using a simple version of “try and catch”, which means it will stop at any error and calls it out in the console.

All you have to do now is upload the content of the VMware Tools package to the new location. The VM’s will automatically compare to this location now and show if they are up-to-date or not.

As with anything found on the internet, test it in a lab environment first!
The full code is available below to copy or to download as a .PS1 file.

Change_VMware_Tools_LocationDownload
 <#
.SYNOPSIS
    Change_VMware_Tools_Location.ps1 - PowerShell Script to change the VMware Tools location to a central one (like a datastore)
.DESCRIPTION
    This script is used to alter the default VMware Tools location to a central one (for all ESXi hosts).
    It will list a grid view of all datastores so you can select the one to use and prompts you for the name of the folder to create.
    The VMware Tools files itself will not be uploaded, this is a manual action.

    This script only works on vSphere 6.7 Update 1 and newer. Older versions require a different method for changing the VMware Tools location!
.OUTPUTS
    Results are printed to the console.
.NOTES
    Author        Jesper Alberts, Twitter: @jesperalberts, Blog: www.vjal.nl

    Change Log    V1.00, 12/03/2020 - Initial version
#>

$ErrorActionPreference = "Stop"

try {

    # Ask the user for the required information to connect to vCenter
    $vCenterServer = Read-Host "Enter the vCenter Server hostname"
    $AdminUsername = Read-Host ("Enter the username for the administrative account")
    $AdminPassword = Read-Host ("Password for " + $AdminUsername) -AsSecureString:$true
    $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $AdminUsername, $AdminPassword

    # Connect to the specified vCenter Server
    Connect-viserver -Server $vCenterServer -Credential $Credentials
    # Specify different variables used in the script
    $ESXiHosts = Get-VMHost
    $DSName = (Get-Datastore | Out-GridView -PassThru).Name
    $DSFolder = Read-Host ("Enter the folder name for the VMware Tools Repository")
    $DSPath = "/vmfs/volumes/$DSName"
    $Location = "$DSPath/$DSFolder"

    # Start with testing if the directory exists, if not it will be created. Otherwise the VMware Tools location will be changed.
    Write-Output "Checking if the folder already exists on the datastore"
    $Datastore = Get-Datastore -Name "$DSName"
    New-PSDrive -Location $Datastore -Name DS -PSProvider VimDatastore -Root "\" | Out-Null
    $CheckPath = Test-Path "DS:\$DSFolder"

    If ($CheckPath -match "False") {
      Write-Output "The folder '$DSFolder' on datastore '$DSName' does not exist, creating it now."
      # Creating the folder on the datastore
      New-Item -Path "DS:\$DSFolder" -ItemType Directory | Out-Null
      #  Disconnects the earlier created PSDrive
      Remove-PSDrive -Name DS -Confirm:$false | Out-Null
    }
    ForEach ($ESXiHost in $ESXiHosts) {
      $esx = Get-VMHost -Name $ESXiHost
      $OldLocation = $esx.ExtensionData.QueryProductLockerLocation()
      $esx.ExtensionData.UpdateProductLockerLocation($Location) | Out-Null
      Write-Output "Changed VMware Tools location on $ESXiHost from $OldLocation to $Location"
    }
    Disconnect-VIServer -Confirm:$False | Out-Null
  }
  catch {
    Write-Warning $Error[0]
  }

Please follow and like us:
Tweet

PowerCLI
PowerCLI, VMware Tools, VMware vSphere

Post navigation

PREVIOUS
My VMware VCAP7-DTM Design (3V0-752) exam experience (remotely proctored)
NEXT
My VMware VCAP6-DTM Deploy (3V0-653) experience

Leave a Reply Cancel reply

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

Let’s stay in touch!

Twitter
LinkedIn

Blog sponsors

Recent Posts

  • Vembu BDRSuite Backup for Microsoft 365 v5.1 hands-on experience [sponsored]
  • Deploying and configuring the NVIDIA DLS licensing appliance
  • Quick tip: Controlling SEGv2 cipher suites and scoring an A+ at SSL Labs
jesperalbertsJesper Alberts@jesperalberts·
23 May

It looks like @AgeRoskam, and I will be presenting at the @vmugnl about the @WorkspaceONE suite and how modern management can positively impact your daily operations while still providing a kick-ass employee experience! #vExpertEUC #ITQlife

The Dutch VMUG@vmugnl

De eerste #VMUGNL sessies zijn geaccepteerd en de sprekers zijn al geïnformeerd.

Gefeliciteerd en tot 14 juni in Utrecht.

Houd onze social media in de gaten, binnenkort delen we de agenda.

En schrijf je in op: http://vmugticketsemea.nl

Reply on Twitter 1528698463722123267Retweet on Twitter 15286984637221232675Like on Twitter 152869846372212326724Twitter 1528698463722123267
Retweet on TwitterJesper Alberts Retweeted
impauldirmannPaul Dirmann@impauldirmann·
22 May

If you're attempting your #vcdx, from one candidate to another I highly recommend joining the #slack channel and forming up a study group with others! In my case, it has proven to be helpful 10x over!

#vexpert #vcdx #vmware #dirmanntech

Reply on Twitter 1528403586958475265Retweet on Twitter 15284035869584752657Like on Twitter 152840358695847526510Twitter 1528403586958475265
jesperalbertsJesper Alberts@jesperalberts·
20 May

And that's a wrap for the first week of #CALAAC. Wow! This course exceeds all my expectations in any possible way. I'm tired but satisfied. Time to charge the battery and do some homework! #ITQlife

Reply on Twitter 1527645079887912960Retweet on Twitter 15276450798879129601Like on Twitter 152764507988791296013Twitter 1527645079887912960
jesperalbertsJesper Alberts@jesperalberts·
18 May

Hi @aprAmac! De Logitech Spotlight Presenter (910-004861) is momenteel nergens op voorraad bij jullie, maar lijkt wel te bestellen. Komt deze binnenkort weer op voorraad?

Reply on Twitter 1526862652785541120Retweet on Twitter 1526862652785541120Like on Twitter 1526862652785541120Twitter 1526862652785541120
jesperalbertsJesper Alberts@jesperalberts·
16 May

Day 1 of #CALAAC is here! #vExpert #ITQlife

Reply on Twitter 1526099039795785729Retweet on Twitter 15260990397957857291Like on Twitter 152609903979578572913Twitter 1526099039795785729
Load More...

Archives

  • March 2022
  • August 2021
  • January 2021
  • October 2020
  • August 2020

Categories

  • BCDR
  • Certification
  • Dynamic Environment Manager
  • Horizon
  • NVIDIA vGPU
  • Partners
  • Personal
  • PowerCLI
  • Secure Email Gateway
  • UAG
  • Uncategorized

Tags

Back-up BCDR Certificates Certification DEM Dynamic Environment Manager Horizon Identity Manager ITQ Job Licensing Microsoft 365 NVIDIA Personal PowerCLI Replication SEG SEGv2 SSL SSLLABS Troubleshooting True SSO UAG Upgrading VCAP VCIX Vembu vGPU VMware Tools VMware vSphere Workspace One Access
© 2022   All Rights Reserved.