Normally when you put a host into Maintenance mode the templates will stay on the host instead of being migrate to a different host. This can be very annoying if you are performing maintenance on the vSphere host and a colleague needs to deploy a VM from the template. I am running vSphere 4.1 update 1. I don’t know if this is still the case with vSphere 5. The host in Maintenance mode will look like this:
So to fix this annoying “issue” I have created a PowerCLI function to place the vSphere host into maintenance mode and if there are Templates registered on the vSphere host, the Templates will be moved to another host in the Cluster.
Function Enter-MaintenanceMode{ <# .SYNOPSIS Enter Maintenance mode .DESCRIPTION The function starts the Enter Maintenance task and also migrates the Templates to another host. .NOTES Author: Arne Fokkema .PARAMETER vmHost One vmHosts. .EXAMPLE PS> Enter-MaintenanceMode<vmHost Name> .EXAMPLE PS> Get-VMHost <vmHost Name> | Enter-MaintenanceMode #> [CmdletBinding()] param( [parameter(ValueFromPipeline = $true, position = 0, Mandatory = $true, HelpMessage = "Enter the vmHost to start the Enter Maintenance mode task")] $vmHost ) $templates = Get-VMHost $vmHost | Get-Template if($templates -eq $null){ $tplMigrate = $false } else{ $tplMigrate = $true } $targetVMHost = Get-VMHost -Location (Get-Cluster -VMHost (Get-VMhost $vmHost)).Name | Where {$_.Name -ne $vmHost} | Sort Name | Select -First 1 if($tplMigrate -eq $true){ foreach($tpl in $templates){ Write-Host "Converting template $($tpl.Name) to VM" -ForegroundColor Yellow $vm = Set-Template -Template (Get-Template $tpl) -ToVM Write-Host "Moving template $($tpl.Name) to vmHost: $($targetVMHost)" -ForegroundColor Yellow Move-VM -VM $vm -Destination (Get-VMHost $targetVMHost) -Confirm:$false | Out-Null Write-Host "Converting template $($tpl.Name) back to template" -ForegroundColor Yellow ($vm | Get-View).MarkAsTemplate() | Out-Null } } Write-Host "Enter Maintenance mode $($vmHost)" -ForegroundColor Yellow Set-VMHost $vmHost -State Maintenance | Out-Null }
You can run the script like this:
Enter-MaintenanceMode esx07
Or from the pipeline:
Get-VMHost esx07 | Enter-MaintenanceMode
The output will be the same:
And the host is completely empty and ready for maintenance: