Got tired of creating every. single. horizon. pool… and here we are 🙂
This is another take on Roderik De Block’s PowerCLI script, you can find that script here: https://roderikdeblock.com/create-horizon-desktop-pool-using-powercli/
As i’m running all my automation stuff on my trusty Ubuntu 22.04 server, I ran into a few issues with using PowerShell to run Rod’s script. I’ll post what I did differently for my own environment, if you want the full script, head to Rod’s blog and check out the script. He deserves that traffic!
Instead of having to type your username/password each time you run the script, i’ve created a horizon.cred file that contains this information, encrypted.
First generate your horizon.cred file. Open a powershell window and type
$credential = Get-Credential
Enter your username/password. This will store that information inside the $credential variable.
Now export it to a file by typing
$credential = Import-Clixml -Path ./horizon.cred
If you want to use horizon.cred with your script, simply paste that code in the beginning of your powershell. This will set the variable $credentials to look up the horizon.cred, which contains your creds!
Now because I have multiple pools, I want to have a script that needs to be idempotent. The script will lookup variables store in a file. This is the piece of code you need
# Read the contents of the file
$FileContents = Get-Content -Raw -Path "folder/your_variables.txt"
# Execute the variable assignments
Invoke-Expression $FileContents
#Let's import the required modules to make the magic work
Import-Module VMware.VimAutomation.HorizonView
Import-Module -name /opt/microsoft/powershell/7/Modules/VMware.Hv.Helper/VMware.HV.Helper.psm1
Write-Host "HVServer: $HVServer"
Write-Host "Vcenter: $Vcenter"
Write-Host "Datacenter: $Datacenter"
Write-Host "Datastores: $Datastores"
Write-Host "DomainAdmin: $DomainAdmin"
Write-Host "HostOrCluster: $HostOrCluster"
Write-Host "ResourcePool: $ResourcePool"
Write-Host "NetBiosName: $NetBiosName"
Write-Host "ParentVM: $ParentVM"
Write-Host "SnapshotVM: $SnapshotVM"
Write-Host "PoolType: $PoolType"
Write-Host "ProductionFolder: $ProductionFolder"
Only piece of code that gave me issues was the VMware.Hv.Helper module, which doesn’t seem to load when I list the available modules. So I just load it directly from the .psm1 file instead, as shown above.
You’ll notice that I have a $PoolType in there. That’s because we can specify if it’s a PRODUCTION or TEST implementation for the pool. Cool!
#Select Environment
$Environment = "$PoolType"
Now, your variable file your_variables.txt should look like this
# Main variables
$HVServer = "192.168.1.X"
$Vcenter = "192.168.1.X"
$Datacenter = "Datacenter"
$Datastores = "Storage"
$DomainAdmin = "YourServiceAccount"
$HostOrCluster = "192.168.1.X"
$ResourcePool = "datacenter"
$NetBiosName = "YOUR_DOMAIN"
$PoolType = "PRODUCTION" #Can also be TEST
# VM variables
$ParentVM = "Ubuntu 20.04 Template"
$SnapshotVM = "2023-09-11-V1"
$ProductionFolder = "/datacenter/vm/Templates"
And that’s it! Nerd it out 🙂