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

    <#
        .SYNOPSIS
        Lists virtual machines configured with multi-writer virtual disk sharing across all vCenter appliances attached to SDDC Manager.

        .DESCRIPTION
        Queries virtual disk inventory across all vCenter appliances connected to SDDC Manager using
        Get-VcfCheckHardDiskInventory.

        Inspects the underlying vSphere API disk backing property (`ExtensionData.Backing.Sharing`)
        to identify virtual disks configured with `sharingMultiWriter`. Multi-writer sharing prevents
        vMotion and Storage vMotion operations, which can impede host evacuation during lifecycle tasks.

        Evaluation logic:
        - Pass: No virtual disks are configured with multi-writer sharing.
        - Warning: One or more virtual disks have multi-writer sharing enabled.

        Populates a structured Rows table detailing VMName, Host, DiskName, Sharing, and Datastore.
        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_multiwriter_enabled_vm' -Area vCenter -DisplayName $DisplayName -Body {
        param($Context, $VCenterFqdn)

        $disks = @(Get-VcfCheckHardDiskInventory -Server $VCenterFqdn)
        $multiwriterDisks = @($disks | Where-Object { $_.ExtensionData.Backing.Sharing -eq 'sharingMultiWriter' })

        if ($multiwriterDisks.Count -gt 0) {
            $rows = @($multiwriterDisks | ForEach-Object {
                [PSCustomObject]@{
                    VMName = $_.Parent.Name
                    Host = $_.Parent.VMHost.Name
                    DiskName = $_.Name
                    Sharing = $_.ExtensionData.Backing.Sharing
                    Datastore = $_.Filename
                }
            })
            $vmNames = ($multiwriterDisks | ForEach-Object { $_.Parent.Name } | Select-Object -Unique) -join '; '
            return [PSCustomObject]@{ Status = 'Warning'; Detail = "Multiwriter-enabled disk(s) found on: $vmNames"; Rows = $rows }
        }
        return [PSCustomObject]@{ Status = 'Pass'; Detail = "Checked $($disks.Count) virtual disk(s); none are multiwriter-enabled."; Rows = @() }
    }
}