I was recently asked if it was possible to automate the update of a View Pool every week after the updates had been applied to the virtual machines using Windows Update services, the process is quite straight forward.
This person needed to snapshot a VM after the updates were completed and then use this snapshot to re-compose the pool, after this was complete they would then use this pool to make sure there were no issues.
The following script does just that, it takes the VM from a given pool and snapshots the VM, it then adds this snapshot back as the active replica for the pool, the idea is to run this as a scheduled task (maybe on Tuesday night) on the View server allowing the snapshot to be created every time it runs.
Watch it in action…
The Script…
$VIServer = "192.168.0.11" $PoolID = "VDIPool" function Get-SnapshotPath { param( $Snapshot ) PROCESS { if ($Snapshot) { $SnapPath = $Snapshot.Name do { $SnapPath = "$($snapshot.Parent.Name)/" + $SnapPath $Snapshot = $Snapshot.Parent } while ($Snapshot.Parent -ne $null) $SnapPath = "/" + $SnapPath $SnapPath } Else { Write-Error "Snapshot not found" } } } if (!(get-pssnapin -name VMware.VimAutomation.Core -erroraction silentlycontinue)) { add-pssnapin VMware.VimAutomation.Core } if (!(get-pssnapin -name VMware.View.Broker -erroraction silentlycontinue)) { add-pssnapin VMware.View.Broker } Connect-VIServer $VIServer Write-Host "Retrieving Pool" $Pool = Get-Pool -pool_id $PoolID Write "$($Pool.displayname) is currently using $($Pool.ParentVMPath) and snapshot $($Pool.parentVMSnapshotPath)" Write-Host "Retreiving VM" $VMName = ($Pool.parentVMPath).Split("/")[-1] $VM = Get-VM $VMName Write-Host "Creating Snapshot" $NewImage = $VM | New-Snapshot -Name "Automated Snapshot" -Description "Automatically created by re-composure script" Write-Host "Updating $($pool.displayname)" $SnapshotPath = Get-SnapshotPath $NewImage Update-AutomaticLinkedClonePool -pool_id $PoolID -parentSnapshotPath $SnapshotPath $Pool = Get-Pool -pool_id $PoolID Write-Host -ForegroundColor Green "$($Pool.displayname) has been updated to use $($Pool.ParentVMPath) and snapshot $($Pool.parentVMSnapshotPath)" Write-Host "Recomposing $($Pool.displayname)..." $Recompose = Get-DesktopVM -pool_id $PoolID | Send-LinkedCloneRecompose -parentSnapshotPath $SnapshotPath -schedule (Get-Date) -parentVMPath $Pool.parentVMPath Write-Host "Completed"
This script grabs the VM used in the pool and creates a snapshot on it, next time it is run it will create another snapshot, and another and another, if you were running this regularly then I would suggest adjusting it to commit the snapshot before creating the next one.
What happens with the script if you run it again the next month? Will it know to use the new snapshot, even though the name will be the same?