Hi Loïc
Once you have defined your cluster image, you can leverage the vCenter GUI or PowerCLI to remediate individual hosts. Using PowerCLI requires a little bit more work for error handling, but here is the basic function to do so
function Wait-ForTask ($taskName) {
do {
$tasks = Get-Task | Where-Object { $_.ObjectId -match $clusterId -and $_.Name -eq $taskName }
if ($tasks.State -contains 'Running') {
Write-Host "$taskName is still running on $($vmHost.Name)"
Start-Sleep -Seconds 60
}
} while ($tasks.State -ne 'Success')
}
function Invoke-VMHostRemediation ($vmHosts) {
foreach ($vmHost in $vmHosts) {
$hostId = $vmHost.ExtensionData.MoRef -replace 'HostSystem-', ''
#Put the host into Maintenance Mode
Set-VMHost -VMHost $vmHost.Name -State Maintenance -Confirm:$false
#Ensure the host is in Maintenance mode
do {
Start-Sleep -Seconds 60
Write-Host "Validating that $($vmHost.Name) is in Maintenance Mode"
$maintenanceModeCheck = Get-VMHost -Name $vmHost.Name
} until ($maintenanceModeCheck.ConnectionState -eq 'Maintenance')
$spec = $esxClusterSoftwareService.Help.'apply$task'.spec.Create()
$spec.accept_eula = $true
$spec.hosts = @($hostId)
# Run vLCM Apply/Patch Operation
Write-Host "Initiating remediation for $($vmHost.Name)"
$esxClusterSoftwareService.'apply$task'($clusterId, $spec)
Wait-ForTask -taskName 'Remediate Cluster'
Wait-ForTask -taskName 'Check compliance of cluster with image'
#Take the Host out of Maintenance Mode
Write-Host "$($vmHost.Name) is exiting Maintenance Mode"
Set-VMHost -VMHost $vmHost.Name -State Connected -Confirm:$false | Out-Null
Start-Sleep -Seconds 15
}
}
$vCenter = 'vCenterName.FQ.DN'
$cluster = 'ClusterName'
Connect-VIServer -Server $vCenter
Connect-CisServer -Server $vCenter
$clusterId = (Get-Cluster $cluster | select -ExpandProperty Id) -replace 'ClusterComputeResource-', ''
$vmHosts = Get-View -ViewType HostSystem | Where-Object { $_.Parent -match $clusterId }
$esxClusterSoftwareService = Get-CisService -Name com.vmware.esx.settings.clusters.software
Invoke-VMHostRemediation -vmHosts $vmHosts