VMware Cloud Community
nikpetrov95
Contributor
Contributor

Enter esxi to mm if memory host status is problem


Hello @LucD , please help,I don't know how to implement host status check

$vmhosts=get-vmhost ((Get-AlarmDefinition -Name "Test alarm*").Entity)|?{$_.ConnectionState -eq "Connected"}
foreach ($vmhost in $vmhosts)
{
$vmhostCluster=Get-Cluster $vmhost.parent
if (($vmhostCluster|Get-VMHost|?{$_.State -ne "Connected"}).count -eq 0){Set-VMHost -VMHost $vmhost -State "Maintenance"}

next I want the email to be sent if the host Maintenance

If ( $  ) {$Body ="<h2> Host $ vmhost is faulty, enter to MM is not possible because another host is already output to MM. </h2>"} else {$ Body = "<h2> Host $ vmhost is faulty, enter to MM is starting </ h2> "}}

0 Kudos
22 Replies
LucD
Leadership
Leadership

I definitely do NOT agree with running any script on your VCSA !!!

If you can run this on any other station, you could  do

$alarmName = 'Memory Usage'
$alarm = Get-AlarmDefinition -Name $alarmName

$report = @()

Get-Cluster -PipelineVariable cluster |
ForEach-Object -Process {
    $esx = Get-VMHost -Location $cluster
    $mm = $esx | where{$_.ConnectionState -eq 'Maintenance'}
    $alarmFired = $esx | where{$_.ExtensionData.TriggeredAlarmState.Alarm -eq $alarm.ExtensionData.MoRef}
    if($alarmFired.Count -gt 0){
        $obj = New-Object -TypeName PSObject -Property @{
            Cluster = $cluster.Name
            AlarmFired = $alarmFired.Name -join '|'
            MaintenceMode = ''
        }
        if($mm.Count -eq 0){
            $tgtEsx = $alarmFired | Get-Random
            Set-VMHost -VMHost $tgtEsx -State Maintenance -Evacuate -Confirm:$false
            $obj = MaintenceMode = $tgtEsx.Name
        }
        else{
            $obj = MaintenceMode = "$($mm.Name -join '|') already in maintenance mode"
        }
        $report += $obj
    }
}

$sMail = @{
    To         = 'me@domain'
    From       = 'vcsa@domain'
    Subject    = 'Alarm action - maintenance mode'
    Body       = $report | ConvertTo-Html -Head $Header | Out-String
    BodyAsHtml = $true
    Priority   = 'High'
    SmtpServer = 'mail.domain'
}
Send-MailMessage @smail

 


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

0 Kudos
nikpetrov95
Contributor
Contributor

Thanks for the help!

Is it possible to change the script and take it as a basis?

This

$vmhosts=get-vmhost ((Get-AlarmDefinition -Name "Test alarm*").Entity)|?{$_.ConnectionState -eq "Connected"}
foreach ($vmhost in $vmhosts)
{
$vmhostCluster=Get-Cluster $vmhost.parent
if (($vmhostCluster|Get-VMHost|?{$_.State -ne "Connected"}).count -eq 0){Set-VMHost -VMHost $vmhost -State "Maintenance"}

or

 

$vmhosts=get-vmhost ((Get-AlarmDefinition -Name "Test alarm*").Entity)|?{$_.ConnectionState -eq "Connected"}
$report = @()
foreach ($vmhost in $vmhosts)
{
$esxcli = Get-EsxCli -VMHost $vmhost
$vmhostCluster=Get-Cluster $vmhost.parent

if (($vmhostCluster|Get-VMHost|?{$_.State -ne "Connected"}).count -eq 0)($esxcli.system.maintenanceMode.set($true,60,$null)){

$report += New-Object PSOBject -Property @{

Log = "$($vmhost) OK"

}

}

else{

$report += New-Object PSObject -Property @{

Log = "$($vmhost) NOK"

}

}

}


 

0 Kudos
LucD
Leadership
Leadership

I have no idea what you are asking here.


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

0 Kudos