PowerCLI

 View Only
  • 1.  Script to extract host in maintenance mode

    Posted Jul 09, 2020 08:16 AM

    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.



  • 2.  RE: Script to extract host in maintenance mode

    Posted Jul 09, 2020 08:21 AM

    What do you already have?



  • 3.  RE: Script to extract host in maintenance mode

    Posted Jul 09, 2020 09:27 AM

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



  • 4.  RE: Script to extract host in maintenance mode

    Posted Jul 09, 2020 12:28 PM

    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