Private/Checks/VCenter/Test-VcfVcenterServiceStatusCheck.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-VcfVcenterServiceStatusCheck {

    <#
        .SYNOPSIS
        Checks that no vCenter appliance service is reporting an "unknown" state.

        .DESCRIPTION
        Queries vCenter Server services state across all vCenter appliances connected to SDDC Manager
        using Get-VcfCheckVCenterServices.

        Evaluates the reported state for each service:
        - Pass: All vCenter services report a defined state and none report 'unknown'.
        - Warning: One or more services report an 'unknown' state.

        Populates a structured Rows table detailing ServiceId and State.
        Delegates per-vCenter execution and per-domain outcome packaging to Invoke-VcfCheckPerVCenterCheck.

        .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_service_status_check' -Area vCenter -DisplayName $DisplayName -Body {
        param($Context, $VCenterFqdn)

        $services = Get-VcfCheckVCenterServices -Server $VCenterFqdn

        $unknownServices = [System.Collections.Generic.List[String]]::new()
        $rows = @()
        foreach ($serviceId in $services.Keys) {
            $state = [String]($services[$serviceId].State)
            $rows += [PSCustomObject]@{
                ServiceId = $serviceId
                State = $state
            }
            if ($state.ToLowerInvariant() -eq 'unknown') {
                $unknownServices.Add($serviceId)
            }
        }

        if ($unknownServices.Count -gt 0) {
            $filteredRows = @($rows | Where-Object { $_.State -eq 'unknown' })
            return [PSCustomObject]@{ Status = 'Warning'; Detail = "Service(s) reporting an unknown state: $($unknownServices -join '; ')"; Rows = $filteredRows }
        }
        return [PSCustomObject]@{ Status = 'Pass'; Detail = "Checked $($services.Keys.Count) service(s); none report an unknown state."; Rows = $rows }
    }
}