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

    <#
        .SYNOPSIS
        Checks for inconsistent IPFIX/NetFlow configuration across distributed virtual switches on every vCenter attached to SDDC Manager.

        .DESCRIPTION
        Queries distributed virtual switch (VDS) inventory across all vCenter appliances connected
        to SDDC Manager using Get-VcfCheckVDSwitchInventory.

        Evaluates IPFIX/NetFlow collector configuration across all switches based on whether a collector
        IP address is assigned:
        - Pass: IPFIX collector configuration is consistent (either enabled on all switches or disabled on all switches).
        - Warning: IPFIX configuration is inconsistent (enabled on some switches and disabled on others).
        - Skipped: No distributed virtual switches are found in the vCenter inventory.

        Populates a structured Rows table detailing SwitchName, IPFIXEnabled, and CollectorIP.
        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_ipfix_netflow_enabled' -Area vCenter -DisplayName $DisplayName -Body {
        param($Context, $VCenterFqdn)
        $vdSwitches = @(Get-VcfCheckVDSwitchInventory -Server $VCenterFqdn)

        if ($vdSwitches.Count -eq 0) {
            return [PSCustomObject]@{ Status = 'Skipped'; Detail = 'No distributed virtual switches found.'; SkipReasonTag = 'no distributed switches'; Rows = @() }
        }

        $enabled = @($vdSwitches | Where-Object { -not [String]::IsNullOrEmpty($_.ExtensionData.Config.IpfixConfig.CollectorIpAddress) })
        $disabled = @($vdSwitches | Where-Object { [String]::IsNullOrEmpty($_.ExtensionData.Config.IpfixConfig.CollectorIpAddress) })

        $rows = @($vdSwitches | ForEach-Object {
            $collectorIp = $_.ExtensionData.Config.IpfixConfig.CollectorIpAddress
            [PSCustomObject]@{
                SwitchName = $_.Name
                IPFIXEnabled = -not [String]::IsNullOrEmpty($collectorIp)
                CollectorIP = if ([String]::IsNullOrEmpty($collectorIp)) { 'Not configured' } else { $collectorIp }
            }
        })

        if ($enabled.Count -gt 0 -and $disabled.Count -gt 0) {
            $enabledNames = ($enabled | Select-Object -ExpandProperty Name) -join '; '
            $disabledNames = ($disabled | Select-Object -ExpandProperty Name) -join '; '
            return [PSCustomObject]@{ Status = 'Warning'; Detail = "IPFIX enabled on: $enabledNames. IPFIX not enabled on: $disabledNames."; Rows = $rows }
        }

        $summary = if ($enabled.Count -gt 0) { "IPFIX is enabled on all $($vdSwitches.Count) distributed virtual switch(es)." } else { "IPFIX is not enabled on any of the $($vdSwitches.Count) distributed virtual switch(es)." }
        return [PSCustomObject]@{ Status = 'Pass'; Detail = $summary; Rows = $rows }
    }
}