A Better Script to Perform Horizon Instant Clone Maintenance using PowerCLI (Part 2)

In Part 1, I showed how to put hosts into Instant Clone maintenance mode using the PowerCLI and VMware.Hv.Helper. In this part, we’ll be finishing off these commands sets with the opposite end of the functions, which is taking hosts out of maintenance mode.

First up, before we enable the pool we need to take the host(s) out of Instant Clone maintenance mode. To do that, we need to clear the value of the custom attribute for that host. (Aside: SUPER annoying you can’t do this in the HTML5 client, but only the old flash-based Web Client.) There are 3 possibilities for this value, in case you’re not aware, and the CLI utilities that are dropped on the Connection Server are doing this exact same thing – setting it or clearing it.

If you set the value to 1, it will initiate IC maintenance. Once it’s completed it will set the value to 2 automatically. Clearing the value with indicate to Horizon that the host is not in maintenance. So for this script, we’ll simply clear the value using the Set-Annotation function in PowerCLI:

if (-not (Get-Annotation -Entity (Get-VMHost -Name $VMhost) -CustomAttribute "InstantClone.Maintenance").Value -eq "") 
{ Set-Annotation -Entity (Get-VMHost -Name $VMhost) -CustomAttribute "InstantClone.Maintenance" -Value "" | Out-Null

Notice we only do this if the value isn’t already clear, so you could certainly pass this to an array of host names that you retrieve. I only have 2 hosts, so I do it manually currently. (Future scripting project!) Also notice that I usually output these to Out-Null so that we have clean entries on the screen.

This can actually take a few minutes, per host, so I usually throw in a waiting period.

Set-VMhost $VMhost -State Connected | Out-Null 
Write-Host "Waiting For Hosts..."
Start-Sleep -Seconds 180

Put it all together, and it looks like this:

Function ICMaint-Stop {
  [CmdletBinding()]
  Param
  (
    [Parameter(Mandatory=$true, Position=0)]
    [string]$VMhost
  )
  Write-Host "Exiting Instant Clone Maintenance Mode..."
  if (-not (Get-Annotation -Entity (Get-VMHost -Name $VMhost) -CustomAttribute "InstantClone.Maintenance").Value -eq "") 
  {
    Set-Annotation -Entity (Get-VMHost -Name $VMhost) -CustomAttribute   "InstantClone.Maintenance" -Value "" | Out-Null
  } else 
  { Write-Host "Instant Clone Maintenance OFF" }
  Set-VMhost $VMhost -State Connected | Out-Null
  Write-Host "Waiting For Hosts..."
  Start-Sleep -Seconds 180
  Write-Host "Finished Instant Clone Maintenance!"
}

NOTE: You’ll want to take your hosts out of maintenance mode at this point if you do that as part of your regular shutdown process. I do not do this, as it’s really not necessary, but in case you do. This is the point where you’d do it in your script.

Now that we’ve taken our host(s) out of maintenance mode, it’s time to re-enable our instant clone desktop pool. This will spin the parents back up and start churning out instant clones quickly thereafter. The template and replica (per host) should still be in place as they remain powered off.

Fortunately, this is very simple with the VMware.Hv.Helper. All we do to kick things off is enable the pool, then start (enable provisioning) the pool.

Function ICPool-Enable {
  [CmdletBinding()]
  Param
  (
    [Parameter(Mandatory=$true, Position=0)]
    [string]$HZpool
  )
  Write-Host "Enabling Pool $($HZpool)..."
  Set-HVPool -PoolName $HZpool -Enable
  Set-HVPool -PoolName $HZpool -Start
  Write-Host "$($HZpool) enabled!"
}

That’s it! You would execute it something like this

ICMaint-Stop <host>
ICPool-Enable <poolid>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close