Compliance VMware HostProfile using a PowerCLI Script
This script can be use to enter ESXi host into maintenance mode, disable alarms on the host, then remediate host profile compliance and after that host will exit from maintenance mode and last enable alarms. Make sure you have base host profile configured, host answer file updated and identical server hardware used in the vsphere cluster.
Provide the hostprofile name, vcenter hostname or IP and enter the list of servers in the “esxnames.txt” file and execute the script.
Hope this will save lot time for some one has a similar requirement.
[string]$HOSTPROFILE = “Host-Profile-Name”
Connect-VIServer vCenter Name or IP
Get-Content -Path .\esxnames.txt -PipelineVariable esxName |
ForEach-Object -Process {
$esx = Get-VMHost -Name $esxName
$alarmMgr = Get-View AlarmManager
#Disable Host Alarms
$alarmMgr.EnableAlarmActions($esx.Extensiondata.MoRef,$false)
write-host “Alarms disabled on the host $esx”
# Enter Host into Maintenance Mode
write-host “Entering Host $esx into Maintenance Mode”
Set-VMHost -VMHost $esx -State Maintenance -Confirm:$false -RunAsync | Out-Null
while($esx.State -ne 'Maintenance'){
sleep 5
$esx = Get-VMHost -Name $esx.Name
}
#Remediate HostProfile
write-host ” “
write-host “Remediate in progress for the host $esx”
Apply-VMHostProfile -Entity $esx -Profile $HOSTPROFILE -Confirm:$false
# Exit Host From Maintenance Mode
write-host ” “
write-host “The host $esx remediate completed”
write-host ” “
write-host “Exit Host $esx From Maintenance Mode”
Set-VMHost -VMHost $esx -State Connected -Confirm:$false -RunAsync | Out-Null
while($esx.State -ne 'Connected'){
sleep 5
$esx = Get-VMHost -Name $esx.Name
#Enable Host Alarms
$alarmMgr.EnableAlarmActions($esx.Extensiondata.MoRef,$true)
write-host “Alarms enabled on the host $esx”
write-host ” ”
write-host “————————————–“
write-host “Continuing to Next Host”
}
} | export-csv -useculture -notypeinformation -path .\hostprofilecompliancereport.csv
write-host “No more hosts.”
write-host “Change completed for the provided hosts.”
write-host “Report Exported.”