VMware Cloud Community
Shiv_
Contributor
Contributor

Power CLI script to put host to Maintenance mode

HI all,

I am working on power CLI script to automate regular tasks and I need some here with putting host to maintenance mode, but there are some prechecks needs to be done on host/cluster before host goes in MM.

*we have DRS set to fully automated 

1. Check if there is any Affinity rule/Vm Rules on host. if there is affinity rule need comment output to user to remove it

2.Check if there is any other host is not responding/powered off or is Maintenance mode in a cluster because as per policy not more than one host can be down in cluster, also if there can be logic to check workload like if other hosts are at 90% utilization on compute resource like Memory and CPU and they cannot take the workload.

if no other host down, target host should go to MM.

3. we use vSAN so VsanDataMigrationMode needs to be set to Full before host goes to MM

 

for 1st I have logic below which will check for affinity rule

$report = foreach($vc in $global:DefaultVIServers){
    foreach ($cluster in Get-Cluster -Server $vc){
        Get-DrsRule -Cluster $cluster|
        Select Name, Type, Enabled,
            @{N='vCenter';E={$vc.Name}},
            @{N='Cluster';E={$cluster.Name}}
    }
}
$report |Export-Csv -Path".\DRSRule-Report-$(Get-Date -Format 'MMdd-HHmm').csv" -NoTypeInformation -UseCulture
 
for 2nd
 
$esxName = 'MyEsx'

$esx = Get-VMHost -Name $esxName

$timeout = 0    # No timeout
$evacuatePoweredOffVms = $true
$spec = New-Object -TypeName VMware.Vim.HostMaintenanceSpec
$spec.VsanMode = New-Object -TypeName VMware.Vim.VsanHostDecommissionMode
$spec.VsanMode.ObjectAction = [VMware.Vim.VsanHostDecommissionModeObjectAction]::noAction

$esx.ExtensionData.EnterMaintenanceMode($timeout, $evacuatePoweredOffVms, $spec)
 
and I am unable to figure out the logic/code for threshold and to check other hosts as I am not into automation,
Any help would be much appreciated.
 
Thanks!

 

 

 

0 Kudos
4 Replies
LucD
Leadership
Leadership

I'm not sure how those snippets actually do anything you describe in the 3 requirements at the start.

Snippet 1 just lists all DRS rules, but doesn't check if the ESXi node you want to place in maintenance mode is in any of the HostGroups used in those rules.

Snippet 2 places an ESXi node in maintenance mode, but for the VSAN migration you select 'noAction', contrary to what your requirement states.

Condition #2 does not appear to be tackled in any of the snippets,


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Shiv_
Contributor
Contributor

HI,

These are existing code used in projects, I just took them for reference and need to modify and test in lab env

I apologies if I confused you with those codes, please ignore them.

but yes the requirement is to check for affinity and hosts in cluster and then to put host in Maintenance mode.

0 Kudos
LucD
Leadership
Leadership

Try something like this

$esxName = 'MyEsx'
$mmReason = 'Reason for maintenance mode'

$esx = Get-VMHost -Name $esxName
$cluster = Get-Cluster -VMHost $esx

$drsRules = Get-DrsRule -Cluster $cluster -VMHost $esx
if($drsRules){
  Write-Host "There are DRS VMHostAffinity rules involving ESXi node $esxName"
  Write-Host "Disable or remove DRS rules"
  $drsRules.Name

}
else{
  $otherEsx = Get-VMHost -Location $cluster | where{$_.Name -ne $esxName}
  $maintenanceEsx = $otherEsx.Where{ $connectionSTate -eq 'mainteance' }
  if($otherEsx.ConnectionState -contains 'Maintenance'){
    Write-Host "ESXi node(s) $($maintenanceEsx.Name -join '|') is/are already in 'maintenance mode' in cluster $($cluster.Name)"
  }
  else{
    $loadEsx = $otherEsx.Where{ $_.CpuUsageMhz / $_.CpuTotalMhz -ge 0.9 -or $_.MemeoryUsageGB / $_.MemeoryTotalGB -ge 0.9 }
    if($loadEsx){
      Write-Host "Too much load on ESXi nodes $($loadEsx.Name -join '|') in cluster $($cluster.Name)"
    }
    else{
      Write-Host "Set ESXi nodes $esxName in cluster $($cluster.Name) in maintenance mode"
      $sMM = @{
        VMHost = $esx
        State = 'Maintenance'
        VsanDataMigrationMode = 'Full'
        Evacuate = $true
        Reason = $mmReason
        Confirm = $false
      }
      Set-VMHost @smm
    }
  }
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Shiv_
Contributor
Contributor

HI,

Many thanks for quick response , I will test this and let you know how it went.

 

0 Kudos