Private/Checks/VCenter/Test-VcfVcenterCheckHosts.ps1

# Copyright (c) 2026 Broadcom. All Rights Reserved.
# Broadcom Confidential. The term "Broadcom" refers to Broadcom Inc.
# and/or its subsidiaries.
#
# =============================================================================
#
# SOFTWARE LICENSE AGREEMENT
#
# Copyright (c) CA, Inc. All rights reserved.
#
# You are hereby granted a non-exclusive, worldwide, royalty-free license
# under CA, Inc.'s copyrights to use, copy, modify, and distribute this
# software in source code or binary form for use in connection with CA, Inc.
# products.
#
# This copyright notice shall be included in all copies or substantial
# portions of the software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# =============================================================================
function Test-VcfVcenterCheckHosts {

    <#
        .SYNOPSIS
        Checks that every ESX host managed by every vCenter attached to SDDC Manager is powered on
        and in a connected state.

        .DESCRIPTION
        Queries host inventory across all vCenter domains attached to SDDC Manager using
        Get-VcfCheckVMHostInventory.

        Evaluates host status:
        - Pass: Every host has PowerState 'PoweredOn' AND ConnectionState 'Connected'.
        - Fail: One or more hosts are powered off, disconnected, or not responding.

        Iterates across all vCenter domains using Invoke-VcfCheckPerVCenterCheck and outputs
        per-domain results containing HostDetails (ClusterName, HostName, PowerState,
        ConnectionState, Status) grouped and rendered per-cluster with an expandable per-host
        breakdown.

        .PARAMETER Context
        The VcfCheck.Context object. Must already be connected to SDDC Manager.

        .PARAMETER DisplayName
        Optional friendly display name for the check result.

        .OUTPUTS
        [PSObject[]] Per-vCenter check results generated by Invoke-VcfCheckPerVCenterCheck.
    #>


    [CmdletBinding()]
    [OutputType([PSObject])]
    Param (
        [Parameter(Mandatory = $true)] [PSObject]$Context,
        [Parameter(Mandatory = $false)] [String]$DisplayName = ''
    )

    return Invoke-VcfCheckPerVCenterCheck -Context $Context -CheckId 'vcenter_check_hosts' -Area vCenter -DisplayName $DisplayName -Body {
        param($Context, $VCenterFqdn)
        $hosts = @(Get-VcfCheckVMHostInventory -Server $VCenterFqdn)
        $hostDetails = @($hosts | Sort-Object -Property @{Expression = { $_.Parent.Name } }, Name | ForEach-Object {
            $isHealthy = ($_.PowerState -eq 'PoweredOn') -and ($_.ConnectionState -eq 'Connected')
            [PSCustomObject]@{
                ClusterName     = $_.Parent.Name
                HostName        = $_.Name
                PowerState      = [String]$_.PowerState
                ConnectionState = [String]$_.ConnectionState
                Status          = if ($isHealthy) { 'Pass' } else { 'Fail' }
            }
        })
        $failed = @($hostDetails | Where-Object { $_.Status -eq 'Fail' })

        if ($failed.Count -gt 0) {
            $hostNames = ($failed | ForEach-Object { "$($_.HostName) ($($_.PowerState))" }) -join '; '
            return [PSCustomObject]@{ Status = 'Fail'; Detail = "Host(s) not powered on and connected: $hostNames"; HostDetails = $hostDetails }
        }
        return [PSCustomObject]@{ Status = 'Pass'; Detail = "Checked $($hosts.Count) host(s); all are powered on and connected."; HostDetails = $hostDetails }
    }
}