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

    <#
        .SYNOPSIS
        Detects whether any vSphere Supervisor (Workload Management) cluster is enabled.

        .DESCRIPTION
        Queries vSphere Supervisor (Workload Management) cluster enablement across all vCenter appliances
        connected to SDDC Manager using Get-VcfCheckSupervisorCluster.

        Evaluates software management modes for enabled Supervisor clusters using
        Get-VcfCheckClusterImageBasedByMoRef:
        - Skipped: No Supervisor/Workload Management clusters are enabled on the vCenter.
        - Warning: Supervisor is enabled on clusters managed by vLCM baselines (VUM).
        - Pass: Supervisor is enabled and all Supervisor clusters are managed by vLCM images.

        Delegates execution across vCenter domains 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_detect_vsphere_supervisor' -Area vCenter -DisplayName $DisplayName -Body {
        param($Context, $VCenterFqdn)
        $supervisorClusters = @(Get-VcfCheckSupervisorCluster -Server $VCenterFqdn)

        if ($supervisorClusters.Count -eq 0) {
            return [PSCustomObject]@{ Status = 'Skipped'; Detail = 'No Supervisor/Workload Management cluster is enabled.'; SkipReasonTag = 'Supervisor Service not installed'; Rows = @() }
        }

        $isImageBasedByMoRef = try {
            Get-VcfCheckClusterImageBasedByMoRef
        } catch {
            @{}
        }

        $rows = @($supervisorClusters | ForEach-Object {
            $clusterId = $_.Cluster.ExtensionData.MoRef.Value
            $isImageBased = if ($clusterId -and $isImageBasedByMoRef.ContainsKey($clusterId)) { $isImageBasedByMoRef[$clusterId] } else { $true }
            [PSCustomObject]@{
                ClusterName      = $_.Cluster.Name
                EnablementState  = $_.EnablementState
                VLcmManagement   = if ($isImageBased) { 'Image' } else { 'Baseline (VUM)' }
            }
        })

        $clusterNames = ($supervisorClusters | ForEach-Object { $_.Cluster.Name }) -join '; '
        $baselineManagedClusterNames = ($rows | Where-Object { $_.VLcmManagement -eq 'Baseline (VUM)' } | ForEach-Object { $_.ClusterName }) -join '; '

        if ($baselineManagedClusterNames) {
            return [PSCustomObject]@{ Status = 'Warning'; Detail = "Supervisor enabled on vLCM baseline (VUM) managed cluster(s): $baselineManagedClusterNames. Managing vCenter: $VCenterFqdn."; Rows = $rows }
        }
        return [PSCustomObject]@{ Status = 'Pass'; Detail = "Supervisor enabled on: $clusterNames"; Rows = $rows }
    }
}