Just last night a fellow TAM asked if we could automate adding vSphere Tags for Horizon desktops. The background is that if a customer uses a backup solution that ties to these tags to also automate what gets backed up so how can do make this easier for them. Not a bad idea, I also have another customer that does the same scenario and figured it’s something that we can do. He posted the question and we ran with it.
First, create a user account and use the prebuilt Role of Tagging Admin.
Default privileges role can be seen here or by going Menu -> Administration -> Roles
At the vCenter level added this user with the Tagging Admin Role and propagated to children.
Next we will create the Tag Category and Tag ahead of time. In my home lab use case just creating a category for VDI and tag of Non-Prod. You get here by going Menu -> Administration -> Tags & Custom Attributes
Here I created the category of “VDI” and gave it “Many Tags” as I may use this later down the road for more than just one type.
Next I created the “Non-prod” tag and tied to the “VDI” category.
The easy part is done and if you wanted you could even script out the creation of tags and categories.
Now we will create the Powershell script that will be created on the Parent of the desktop pool that we will trigger with the Post-Sync of the Horizon pool configuration.
On the parent create a folder where we will keep the PowerShell script and bat file that we will use to kick off the file. The Horizon post-sync process can only use .bat/.vbs/.cmd files.
Below is my VM-Tag.ps1 script. Here you will define the variables for your vCenter, the tag that we created above, the username and password for the local user we created with the Tagging Admin privileges and then the Computer Name. This is critical that the Computer Name of the OS matches what the VM name will be so we can add the tag to vCenter. If your scenario is different then we will need to find another way to match these so we can automate this process. I wasn’t sure the Get-Credentials that I normally use would work since the parent will fork/clone and the encrypted file won’t work on the child VMs. Also didn’t think the Windows Credential Store would either in this scenario but may test it later.
$vc = "vcenter.AD_Domain.net"
$NPTag = "Non-Prod"
$ComputerName = "$env:computername" # GET COMPUTER NAME
$userTag = "tagging_user@vsphere.local"
$passTag = "YourPassword"
# Connect to vCenter with saved creds
Write-host "Connecting to vCenter.."
connect-viserver -Server $vc -user $userTag -password $passTag | Out-Null
Write-host "Adding backup tag for VM.."
# Add Non-Prod vCenter tag to VM
New-TagAssignment -Tag $NPTag -Entity $ComputerName
exit
Next we will need to create the bat file that will kick off the above script, I called my “post-sync.bat” so that I’ll easily know what that is going forward. I wanted this to not cause any problems if it some reason ran slower and the off chance it kicked off when a user logged in. It should never happen that way but this is why I wanted the window to not display any text (@echo off) and start the powershell in a minimized state (/min) as well if you notice in the PowerShell the Out-Null so it doesn’t show the user the output of the vCenter once it connects.
@echo off
start /min powershell c:\scripts\vm-tag.ps1
exit
I wanted this to not cause any problems if it some reason ran slower and the off chance it kicked off when a user logged in. It should never happen that way but this is why I wanted the window to not display any text (@echo off) and start the powershell in a minimized state (/min) as well if you notice in the PowerShell the Out-Null so it doesn’t show the user the output of the vCenter once it connects.
Now we just need to log into the Horizon Admin portal and add our bat file to the Post-Synchronization Script Name field.
This is the only spot you need to make a change if the pool is already created.
The very last task to do is Shutdown the parent, take a snapshot and push that image out to the pool. Once this is done and sessions are cleared for the new Instant Clones to take place you will see the new Tag.
VM level object view.
Tag object view.
Again this was just a quick overview on how you can accomplish automating a task. Let me know if you have any questions, comments or feedback.