Private/Test-FixVerification.ps1

function Test-FixVerification {
    <#
    .SYNOPSIS
        Verifies that a remediation step achieved the desired outcome.
    .DESCRIPTION
        Waits a specified interval then executes a verification script to confirm
        that a fix was successful. Supports retries with configurable wait periods.
        Returns whether the verification passed, the number of attempts, and the final result.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [scriptblock]$VerificationScript,

        [Parameter()]
        [int]$WaitSeconds = 30,

        [Parameter()]
        [int]$MaxRetries = 3,

        [Parameter()]
        [string]$ComputerName
    )

    $attempt = 0
    $verified = $false
    $lastResult = $null
    $lastError = $null

    while ($attempt -lt $MaxRetries -and -not $verified) {
        $attempt++

        Write-Verbose "Verification attempt $attempt of $MaxRetries - waiting $WaitSeconds seconds..."
        Start-Sleep -Seconds $WaitSeconds

        try {
            $lastResult = & $VerificationScript

            if ($lastResult -eq $true -or $lastResult -match '^\s*True\s*$') {
                $verified = $true
            }
            elseif ($lastResult -is [bool]) {
                $verified = $lastResult
            }
            elseif ($null -ne $lastResult) {
                # Treat non-null, non-false result as potentially successful
                # The verification script should return $true/$false explicitly
                $verified = [bool]$lastResult
            }
        }
        catch {
            $lastError = $_.Exception.Message
            Write-Verbose "Verification attempt $attempt failed: $lastError"
            $lastResult = $null
        }

        if (-not $verified -and $attempt -lt $MaxRetries) {
            Write-Verbose "Verification not yet passed. Retrying..."
        }
    }

    [PSCustomObject]@{
        Verified     = $verified
        Attempts     = $attempt
        MaxRetries   = $MaxRetries
        FinalResult  = $lastResult
        LastError    = $lastError
        ComputerName = $ComputerName
        VerifiedAt   = (Get-Date).ToString('o')
    }
}