One task I run into quite often is deploying Instant Clone pools to test one thing or another.
I dont always leave my lab running, so all the pools get deleted , and then recreated when needed.
If you have ever created an instant you know you have to click about 20 settings, while not a monumental task, it is a bit reparative.
So we are going to go through the process of creating our own script , so we only need to click once to deploy our instant clone pool.
You are going to need a couple of things:
Internet access
PowerCLI installed
PowerCLI Example Scripts
(Optional) A dedicated workstation to store your password on.
Ok lets start with installing PowerCLI.
Open power shell as an administrator
And run the following command to install PowerCLI
Install-Module VMware.PowerCLI
Wait for the progress bar to complete the install of the modules
Once Complete run the following to verify it was installed and get the Version number if you need it at some point
Get-Module -Name VMware.PowerCLI -ListAvailable
PowerCLI is now installed, that's it!
Next We need the example scripts zip file. The zip file includes a specific module for horizon that is not built into the module we just installed.
Once this is downloaded, open the Zip, and we are going to extract the VMware.Hv.Helper folder from
the Modules directory.
You can extract this any where you like, however for our example we will Extract it to C:\Scripts for ease of use
At this point we are ready to write our script!
If you have your own powershell IDE then you can just use that, for this post we are going to use ISE.
Click start and search for powershell ISE:
You should now have a blank space to start
Right off the bat we are going to import our modules, PowerCLI , vmware.vimautomation.horizonview (part of PowerCLI), and the VMware.HV.Helper that we downloaded Earlier.
We will also tell PowerCLI to ignore Invalid certificates in case we are using Self Signed certs, we dont want it to sto for that
Import-Module VMware.PowerCLI
Import-Module vmware.vimautomation.horizonview
Import-Module -Name "C:\scripts\VMware.Hv.Helper"
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Next we are going to setup our variables, this will make it easier if the say the Parent VM, or Snapshot change in the future.
#The Name of the Instant Clone Pool
$HZPool = "W11-IC"
#The display name of the Instant Clone Pool
$HZDisplay = "W11-IC"
#The description of the pool displayed in the horizon Admin
$HZDescription = "Windows 11 Insider Preview Instant Clone Pool"
#How are the users assigned to the desktops for this pool
$HZAssign = "FLOATING"
#What is the name of the parent VM
$HZParent = "W11-Insider-Instant-Clone-Parent"
#The Snapshot we need to use
$HZSnap = "UEM-2103 Installed"
#The folder you want to place the VMs in, I Always put my in the root and let it create the sub folder , so I use HomeDatacenter as its the default
$HZFolder = "HomeDatacenter"
#The name of your vSphere cluster you are deploying to
$HZCluster = "AMD"
#If you have resource pools specify this here, else use the name of your cluster
$HZResourcePool = "VDI"
#How are you naming your VMs?
$HZNamingMethod = "PATTERN"
#The naming pattern of your Instant Clones
$HZPattern = "W11-IC-{n}"
#The Datstore you will deploy the VMs to
$HZDatastore = "SYNFS2"
#Your instant clone admin domain you use to join the domain
$HZDomain = "HOME"
#you instant clone admin username
$HZICAdmin = "hzncomposer"
(Optional) Next we are going to use a feature in power CLI that allows us to store our password so we don't have to have it in plain text in our script.
From the command prompt run the following replacing the -Host with your host, -User with your Horizon Admin user and -Password with your password
New-VICredentialStoreItem -Host hacs1.home.lab -User ViewAutoBot -Password "SuperSecretP@$$w0rd"
I put the password in " as powershell uses $ so its the only way to pass these
This will complete any your password is now saved.
You can validate this by running the following and your password should be returned in plain text:
get-VICredentialStoreItem -Host hacs1.home.lab -User ViewAutoBot | Select-Object Password | foreach {$_.password}
So now our next line is to set a variable for our password:
$HZPass = get-VICredentialStoreItem -Host hacs1.home.lab -User ViewAutoBot | Select-Object Password | foreach {$_.password}
And our line to connect to the connection server utilizing the password
*Note if you did not setup the VICredentialStore, the replace the $HZPass variable with your password in plane text
Connect-HVServer -Server hacs1.home.lab -User ViewAutoBot@home.lab -Password $HZPass
Now lets take all of this data we just setup and get the command setup to use it (I formatted it this way for readability).
New-HVPool -InstantClone -PoolName $HZPool `
-PoolDisplayName $HZDisplay `
-Description $HZDescription `
-UserAssignment $HZAssign `
-ParentVM $HZParent `
-SnapshotVM $HZSnap `
-VmFolder $HZFolder `
-HostOrCluster $HZCluster `
-ResourcePool $HZResourcePool `
-NamingMethod $HZNamingMethod `
-NamingPatter $HZPattern `
-Datastores $HZDatastore `
-NetBiosName $HZDomain `
-DomainAdmin $HZICAdmin
2 more lines, the last thing that is done when deploying a pool is assigning users, because this script runs really fast, I have put a 60 sec sleep statement and then Assign the "Domain Users" to the pool.
Start-Sleep -s 60
New-HVEntitlement -Type Group -User "HOME\Domain Users" -ResourceName $HZPool -Confirm:$false
Thats All, now lest run it !
Now you can make a short cut on your desktop with the following to run it at the click of a button:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -ExecutionPolicy Bypass -File "C:\Scripts\W10IC.ps1"
Here is the full script:
Import-Module VMware.PowerCLI
Import-Module vmware.vimautomation.horizonview
Import-Module -Name "C:\scripts\VMware.Hv.Helper"
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
#The Name of the Instant Clone Pool
$HZPool = "W11-IC"
#The display name of the Instant Clone Pool
$HZDisplay = "W11-IC"
#The description of the pool displayed in the horizon Admin
$HZDescription = "Windows 11 Insider Preview Instant Clone Pool"
#How are the users assigned to the desktops for this pool
$HZAssign = "FLOATING"
#What is the name of the parent VM
$HZParent = "W11-Insider-Instant-Clone-Parent"
#The Snapshot we need to use
$HZSnap = "UEM-2103 Installed"
#The folder you want to place the VMs in, I Always put my in the root and let it create the sub folder , so I use HomeDatacenter as its the default
$HZFolder = "HomeDatacenter"
#The name of your vSphere cluster you are deploying to
$HZCluster = "AMD"
#If you have resource pools specify this here, else use the name of your cluster
$HZResourcePool = "VDI"
#How are you naming your VMs?
$HZNamingMethod = "PATTERN"
#The naming pattern of your Instant Clones
$HZPattern = "W11-IC-{n}"
#The Datstore you will deploy the VMs to
$HZDatastore = "SYNFS2"
#Your instant clone admin domain you use to join the domain
$HZDomain = "HOME"
#you instant clone admin username
$HZICAdmin = "hzncomposer"
$HZPass = get-VICredentialStoreItem -Host hacs1.home.lab -User ViewAutoBot | Select-Object Password | foreach {$_.password}
Connect-HVServer -Server hacs1.home.lab -User ViewAutoBot@home.lab -Password $HZPass
New-HVPool -InstantClone -PoolName $HZPool `
-PoolDisplayName $HZDisplay `
-Description $HZDescription `
-UserAssignment $HZAssign `
-ParentVM $HZParent `
-SnapshotVM $HZSnap `
-VmFolder $HZFolder `
-HostOrCluster $HZCluster `
-ResourcePool $HZResourcePool `
-NamingMethod $HZNamingMethod `
-NamingPatter $HZPattern `
-Datastores $HZDatastore `
-NetBiosName $HZDomain `
-DomainAdmin $HZICAdmin
Start-Sleep -s 60
New-HVEntitlement -Type Group -User "HOME\Domain Users" -ResourceName $HZPool -Confirm:$false
Comments
Post a Comment