Automation

 View Only
  • 1.  PowerCLI get Maintenance

    Posted Apr 09, 2020 09:29 PM

    Hi,

    Don't know if someone could help out with this one?

    I am listing out all the ESXi that are currently in Maintenance mode, from there Listing out the Event Tasks for Such Action:

    I'm trying to do a If Statement if the ESXi Host has an Event under 15 Days Ignore,  if it doesn't have a Event Tasks for EnteringMaintenanceMode or is about 15 Days Sent an Email.

    below is what i currently have 

    $user = ''

    $pswd = ''

    $start = (Get-Date).AddDays(-1)

    $srv = Connect-VIServer -Server  -User $user -Password $pswd

    $ESXIHost = (Get-VMHost -Server $srv | where {$_.ConnectionState -eq 'Maintenance'} | Select Name)

    $tasks = Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) | where {$_ -is [VMware.Vim.TaskEvent] -and $_.Info.DescriptionId -eq "HostSystem.enterMaintenanceMode"} |

    Select CreatedTime,@{N="Host";E={$_.Host.Name}}

    foreach($esx in $ESXIHost)

    {

    if( $tasks.Host -eq $esx -and $tasks.CreatedTime -le -15 ) {

        Write-output "Skipping"

        }else{

        Send-MailMessage -From 'User01 <user01@fabrikam.com>' -To 'User02 <user02@fabrikam.com>' -Subject 'Test mail'

        }

    }



  • 2.  RE: PowerCLI get Maintenance
    Best Answer

    Posted Apr 09, 2020 09:55 PM

    If I may suggest an alternative.

    - find all ESXi nodes in maintenance mode

    - look back at events for the last 15 days for each of these ESXi nodes

    - if an event EnteredMaintenanceModeEvent is found, the ESXi node entered maintenance mode less than 15 days ago

         - ignore

    - if no such event is found, the ESXi node entered maintenance mode more than 15 days ago

         - send email

    $user = ''

    $pswd = ''


    $now = Get-Date

    $start = $now.AddDays(-15)


    $srv = Connect-VIServer -Server  -User $user -Password $pswd


    Get-VMHost -PipelineVariable esx -Server $srv | where {$_.ConnectionState -eq 'Maintenance'} |

    ForEach-Object -Process {

        $maintEntered = Get-VIEvent -Entity $esx -Start $start -MaxSamples ([int]::MaxValue) -Server $srv |

            where{$_ -is [VMware.Vim.EnteredMaintenanceModeEvent]}

        if($maintEntered){

                Write-Host "Ignore"

        }

        else{

            Send-MailMessage -From 'User01 <user01@fabrikam.com>' -To 'User02 <user02@fabrikam.com>' -Subject 'Test mail'

        }

    }



  • 3.  RE: PowerCLI get Maintenance

    Posted Apr 10, 2020 08:25 AM

    Thanks, the above works great.