VMware Cloud Community
Daithi_N
Enthusiast
Enthusiast
Jump to solution

PowerCLI get Maintenance

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'

    }

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Daithi_N
Enthusiast
Enthusiast
Jump to solution

Thanks, the above works great.

Reply
0 Kudos