Eric Monjoin
Staff Consulting Architect but also pilot, spending time in front of my computer or flying in the air...

List Parent VMs and Snapshots and push new images by script

I recently had to upgrade a Horizon infrastructure with around 180 pools and different masters, as you can imagine, that could be exhausting first to check which master is in use and what’s the current snapshot as well, so after searching a little bit on the Net, I’ve able to find what I need :

So the first thing is to retrieve master and snapshot used per pools and save it to a CSV file using this script initially created by Ivo Beerens (https://www.ivobeerens.nl/) and slightly modified by me to redirect the output to a CSV file:

<#
.SYNOPSIS
    PoolParentSnapShot.ps1
.VERSION
    1.0
.DESCRIPTION
    Exports all Horizon pools with the parents and active snapshot 
.NOTES
    Author(s): Ivo Beerens (https://www.ivobeerens.nl/)
    Requirements:  
    Make sure the VMware.HV.Helper module is installed, see: https://github.com/vmware/PowerCLI-Example-Scripts
    Copy the VMware.Hv.Helper to the module location.
	Modified by : Eric Monjoin
	Modification : export to csv 
.EXAMPLE
    PS> ./PoolParentSnapShot.ps1
#>

Import-Module VMware.Hv.Helper

#Variables
$conserver = 'myconserver.mydomain.dom'
$domain = 'mydomain.dom'

# Connect to Horizon Connection Server
Write-Output " ", "Connect to the Connection Server" 
Connect-HVServer -Server $conserver -Domain $domain

$pools = (Get-HVPool).base.name

$myCol = @() # Create empty array
ForEach ($pool in $pools) {
        $poolname = Get-HVPool -PoolName $pool
        $row = " " | Select-Object PoolName, Parent, Snapshot 
        $row.PoolName = $pool
        $row.Parent = $poolname.AutomatedDesktopData.VirtualCenterNamesData.ParentVmPath
        $row.Snapshot = $poolname.AutomatedDesktopData.VirtualCenterNamesData.SnapshotPath
        $myCol += $row

    }
#Write-Output $myCol | Format-Table -AutoSize
$myCol | Export-csv .\PoolParentSnapShot.csv

Disconnect-HVServer * -Confirm:$false

This give me this output :

Now, I can modify the file by removing pools I don’t want to update (eg. RDS Desktop pool), removing all path to snapshot and Parent VM and specify the new snapshot I want to deploy (for a single parent VM, all snapshots name should be different as we only specify its name) :

Finally I created this script to automatically push all new images :

<#
.SYNOPSIS
    PushNewImage.ps1
.VERSION
    1.0
.DESCRIPTION
    Push a new images to multiple Horizon Pools 
.NOTES
    Author(s): Eric Monjoin
    Requirements:  
    Make sure the VMware.HV.Helper module is installed, see: https://github.com/vmware/PowerCLI-Example-Scripts
    
.EXAMPLE
    PS> ./PushNewImage.ps1
#>

Import-Module VMware.Hv.Helper

#Variables
$conserver = 'myconserver.mydomain.dom'
$domain = 'mydomain.dom'
$csvfile = ".\PoolParentSnapShot.csv"

# Connect to Horizon Connection Server
Write-Output " ", "Connect to the Connection Server" 
Connect-HVServer -Server $conserver -Domain $domain

$pools = Import-Csv $csvfile -Delimiter ','
ForEach ($pool in $pools) {
$poolname = $($pool.PoolName)
$parentVM = $($pool.Parent)
$snapshot = $($pool.Snapshot)

Start-HVPool -SchedulePushImage -Pool $poolname -LogoffSetting WAIT_FOR_LOGOFF -ParentVM $parentVM -SnapshotVM $snapshot

} 

Disconnect-HVServer * -Confirm:$false

Once variable are correctly specified, just launch the script (task will be done in the background) :

Note :

  • I’d recommend to do it by 50 pools, in our case we pushed it to 170 pools in one shot with few VMs in each and the last 30 failed because of time out. Not a big problem as we ran again the first script to check which one were not updated.
  • Don’t forget to remove any path for the snapshot (so don’t specify previous one) but also for the parent VM otherwise the deployment will fail

You may also like...

Leave a Reply

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