VMware Cloud Community
Kamote26
Contributor
Contributor

Script to extract host in maintenance mode

So we have multiple vCenters around 100 and I'd like to extract all host that are in maintenance mode.

The script will be like:

List of vCenters are saved in txt file and the script will use this text to scan the vCenters.

The result will be extract to excel file, appreciate if theres an option to send via email on html format.

Information should contain name of the host, vcenter, cluster. cpu. memory, uptime, date it was set to mm if possible. esxi version.

Will appreaciate any assistance, i have been searching the net for this kind of script but could not find anything.

0 Kudos
3 Replies
LucD
Leadership
Leadership

What do you already have?


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

0 Kudos
Kamote26
Contributor
Contributor

Honestly i dont have, most powershell that i have are AD and Hyper-V related and they are just one liner very simple script.

0 Kudos
LucD
Leadership
Leadership

Try something like this

Get-Content -Path .\vcenters.txt -PipelineVariable vcName |

ForEach-Object -Process {

    Connect-VIServer -Server $vcName


    $esx = Get-VMHost -Server $vcName -State Maintenance

    Get-VIEvent -Entity $esx -MaxSamples ([int]::MaxValue) |

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

    Group-Object -Property {$_.Host.Name} -PipelineVariable group |

    ForEach-Object -Process {

        Get-VMHost -Name $group.Name |

        Select Name,State,NumCpu,MemoryTotalGB,Version,

            @{N='vCenter';E={$vcName}},

            @{N='Cluster';E={(Get-Cluster -VMHost $_).Name}},

            @{N='UptimeDays';E={[int](New-TimeSpan -Start $_.ExtensionData.Runtime.BootTime -End (Get-Date)).TotalDays}},

            @{N='Entered MM';E={

                $group.Group | Sort-Object -Property CtreatedTime -Descending |

                Select -First 1 | Select -ExpandProperty CreatedTime

        }}

    }

    Disconnect-VIServer -Server $vcName -Confirm:$false

} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


$sMail = @{

    To = 'me@domain'

    From = 'report@domain'

    Subject = 'Maint report'

    SmtpServer = 'mail.domain'

    BodyAsHtml = $true

    Body = Import-Csv -Path .\report.csv -UseCulture | ConvertTo-Html |Out-String

}


Send-MailMessage @sMail


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

0 Kudos