PowerCLI

 View Only

 How can I leverage PowerCLI/PowerShell to remediate a single host within a cluster instead of the entire cluster?

Jump to  Best Answer
p0werShelldude's profile image
p0werShelldude posted Sep 30, 2024 09:24 AM

When using cluster images, we have the ability to remediate an entire cluster using the GUI, or a similar PowerCLI code like

$cluster | Set-Cluster -Remediate -AcceptEULA -RunAsync -Confirm:$false

Within the GUI, we can also remediate a single host within a cluster that uses cluster images instead of the entire cluster. From what I can tell, there doesn't appear to be a PowerCLI cmdlet or parameter flag that can understand and process a single host remediation. I've been digging into the PowerCLI modules to try and reverse engineer a way to do this with no luck. 

So, my question is how can I remediate a single (specified) host that is part of a cluster managed image using PowerCLI or PowerShell? 

p0werShelldude's profile image
p0werShelldude  Best Answer

Thanks to @LucD for pointing me to the VMware API reference material, I was able to find this article on how to leverage a CimServer connection to remediate a single ESXi Host using vLCM. 

$vCenter = 'vCenterName'
$viCredential = Get-Credential 
$cluster = Get-Cluster -Name 'Cluster Name'

Connect-VIServer -Server $vCenter -Credential $viCredential
Connect-CisServer -Server $vCenter -Credential $viCredential

$clusterId = (Get-view -ViewType ClusterComputeResource | Where-Object { $_.Name -match $cluster }).MoRef -replace 'ClusterComputeResource-', ''
$vmHosts = Get-View -ViewType HostSystem | Where-Object { $_.Parent -match $clusterId }
$esxClusterSoftwareService = Get-CisService -Name com.vmware.esx.settings.clusters.software

foreach($vmHost in $vmHosts){
     $hostId = $vmHost.MoRef -replace 'HostSystem-', ''
     Set-VMHost -VMHost $vmHost.Name -State Maintenance -Confirm:$false

     $spec = $esxClusterSoftwareService.Help.'apply$task'.spec.Create()
     $spec.accept_eula = $true
     $spec.hosts = @($hostId)

     $esxClusterSoftwareService.'apply$task'($clusterId, $spec) 

     Set-VMHost -VMHost $vmHost.Name -State Connected -Confirm:$false
}

Just be sure to put in extra error handling to make sure maintenance mode completes before remediation starts, that remediation is successful, etc. 

LucD's profile image
LucD

The Set-Cluster cmdlet doesn't support that option yet.
Did you check with Code Capture which method the Web Client uses?