Notes of a scripter

Setting up VMware Tools Shared Product Locker with PowerShell

With the changes to how VMware Tools will be released, we finally decided to setup Product Locker. These changes were released in September 2015 VMware decided that the tools would be no longer depended on vSphere releases or ESXi builds.

Download VMware Tools here

One of our other VMware engineers found a script (link here) that could do this task, but it was seemed like it was wrote for a smaller environment than what we work with.  We currently have over 100 VMhosts, so we wanted to take a controlled approach to updating this setting throughout the environment.  Also we had to use a different datastore for each cluster since there wasn’t a single one that was attached to all of the VMHosts. So I updated the script to work best for our environment, so it will run one VM cluster at a time.

**When assigning the variables in the script, note that the value for the datastore and the productLocker names are case sensitive.**

$Cluster         = "VMClusterName"
$DataStoreName   = "DataStore_with_Downloaded_VMTools"
$VMhosts         = Get-Cluster $Cluster | Get-VMHost -Datastore $DataStoreName | Where {$_.ConnectionState -eq "Connected"}
$Datastore       = Get-Datastore $DataStoreName
$PL_Folder       = "productLocker"
$plink           = "C:\Scripts\Applications\plink.exe"
$plinkfolder     = Get-ChildItem $plink
$creds           = (Get-Credential -Message "What is the login for your ESXi Hosts?")
$username        = $creds.UserName
$PW              = $creds.GetNetworkCredential().Password


# Full Path to ProductLockerLocation
Write-host "Full path to ProductLockerLocation: [vmfs/volumes/$($datastore.name)/$PL_Folder]" -ForegroundColor Green

# Set value on all hosts that access shared datastore
Get-AdvancedSetting -entity (Get-Cluster $Cluster | Get-VMHost -Datastore $DataStoreName) -Name 'UserVars.ProductLockerLocation'| Set-AdvancedSetting -Value "vmfs/volumes/$($datastore.name)/$PL_Folder" -confirm:$False

# Each host needs to have SSH enabled to continue
$SSHON = @()
 
# Foreach ESXi Host, see if SSH is running, if it is, add the host to the array
$VMHosts | % {
    if ($_ |Get-VMHostService | ?{$_.key -eq “TSM-SSH”} | ?{$_.Running -eq $true}) {
        $SSHON += $_.Name
        Write-host "SSH is already running on $($_.Name). adding to array to not be turned off at end of script" -ForegroundColor Yellow
    }

# if not, start SSH
    else {
        Write-host "Starting SSH on $($_.Name)" -ForegroundColor Yellow
        Start-VMHostService -HostService ($_ | Get-VMHostService | ?{ $_.Key -eq “TSM-SSH”} ) -Confirm:$false
    }
}

# Change directory to Plink location for ease of use
cd $plinkfolder.directoryname
$VMhosts | foreach {

# Run Plink remote SSH commands for each host
Write-host "Running remote SSH commands on $($_.Name)." -ForegroundColor Yellow
Echo Y | ./plink.exe $_.Name -pw $PW -l $username 'rm /productLocker'
Echo Y | ./plink.exe $_.Name -pw $PW -l $username "ln -s /vmfs/volumes/$($datastore.name)/$PL_Folder /productLocker"
}

write-host ""
write-host "Remote SSH Commands complete" -ForegroundColor Green
write-host ""

# Turn off SSH on hosts where SSH wasn't already enabled
$VMhosts | foreach { 
if ($SSHON -notcontains $_.name) {
Write-host "Turning off SSH for $($_.Name)." -ForegroundColor Yellow
Stop-VMHostService -HostService ($_ | Get-VMHostService | ?{ $_.Key -eq “TSM-SSH”} ) -Confirm:$false
} else {
Write-host "$($_.Name) already had SSH on before running the script. leaving SSH running on host..." -ForegroundColor Yellow
}
} 

**Need to also have plink. Link here**

-Stuart

Exit mobile version