2 Replies Latest reply on Apr 10, 2020 1:24 AM by Daithi_N

    PowerCLI get Maintenance

    Daithi_N Novice

      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'

          }

       

       

      }

        • 1. Re: PowerCLI get Maintenance
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          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'

              }

          }

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          1 person found this helpful
          • 2. Re: PowerCLI get Maintenance
            Daithi_N Novice

            Thanks, the above works great.