Private/Checks/Vsan/Test-VcfVsanInaccessibleObjects.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-VcfVsanInaccessibleObjects {

    <#
        .SYNOPSIS
        Checks for vSAN objects reporting an inaccessible, unhealthy, or non-compliant state.

        .DESCRIPTION
        Queries vSAN object inventory across all vCenter appliances connected to SDDC Manager using Get-VcfCheckVsanObjectInventory.

        Evaluates vSAN object health and storage policy compliance:
        - Pass: All vSAN objects report 'healthy' vSAN health and 'compliant' policy status.
        - Fail: One or more vSAN objects report a non-healthy state or non-compliant policy status.
        - Skipped: No vSAN objects are found in the target vCenter inventory.

        Appends Broadcom KB 389599 troubleshooting guidance to failure detail messages.

        Populates a structured Rows table detailing Cluster, VM, VsanHealth, ObjectId, Type, and ComplianceStatus.
        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 'vsan_inaccessible_objects' -Area vSAN -DisplayName $DisplayName -Body {
        param($Context, $VCenterFqdn)

        $vsanObjects = @(Get-VcfCheckVsanObjectInventory -Server $VCenterFqdn)

        if ($vsanObjects.Count -eq 0) {
            return [PSCustomObject]@{ Status = 'Skipped'; Detail = 'No vSAN objects found.'; SkipReasonTag = 'no vSAN objects'; Rows = @() }
        }

        $unhealthyObjects = @($vsanObjects | Where-Object {
            $_.VsanHealth -ne 'healthy' -or ($_.ComplianceStatus -and $_.ComplianceStatus -ne 'compliant')
        })

        if ($unhealthyObjects.Count -gt 0) {
            $rows = @($unhealthyObjects | ForEach-Object {
                [PSCustomObject]@{
                    Cluster = if ($_.ClusterName) { $_.ClusterName } else { 'N/A' }
                    VM = if ($_.VM) { $_.VM.Name } else { 'N/A' }
                    VsanHealth = $_.VsanHealth
                    ObjectId = $_.Id
                    Type = $_.Type
                    ComplianceStatus = $_.ComplianceStatus
                }
            })
            $troubleshootingReference = Get-VcfCheckVsanTroubleshootingKbText -Number '389599' -Url 'https://knowledge.broadcom.com/external/article/389599/vsan-health-service-data-health-vsan-o.html'
            return [PSCustomObject]@{ Status = 'Fail'; Detail = "$($unhealthyObjects.Count) unhealthy object(s) found. See results table below. $troubleshootingReference"; Rows = $rows }
        }

        return [PSCustomObject]@{ Status = 'Pass'; Detail = "Checked $($vsanObjects.Count) vSAN object(s); all are healthy."; Rows = $rows }
    }
}