Private/Read-NSPEntraConnectState.ps1

function Read-NSPEntraConnectState {
    <#
    .SYNOPSIS
        Reads rules, connector identities and the anchor setting locally or through existing remoting.
    #>

    [CmdletBinding()]
    param(
        [ValidatePattern('\A[A-Za-z0-9][A-Za-z0-9.-]{0,252}\z')][string]$ComputerName,
        [pscredential]$Credential
    )
    $read = Get-NSPEntraConnectReadScript
    if ($ComputerName) {
        $options = @{ ComputerName = $ComputerName; ConfigurationName = 'Microsoft.PowerShell'
            ScriptBlock = $read; ErrorAction = 'Stop'
            SessionOption = (New-PSSessionOption -OpenTimeout 15000 -OperationTimeout 60000) }
        if ($Credential) { $options.Credential = $Credential }
        # Existing authenticated WinRM only; never change TrustedHosts or enable remoting.
        $state = @(Invoke-Command @options)
    } else {
        if ($Credential) { throw 'Credential requires ComputerName for remote collection.' }
        $state = @(Invoke-NSPEntraConnectLocalRead)
    }
    $state = @($state | ForEach-Object { if ($_ -is [string]) { $_ | ConvertFrom-Json -ErrorAction Stop } else { $_ } })
    if ($state.Count -ne 1 -or -not $state[0].ComputerName -or -not $state[0].ModuleVersion -or
        $null -eq $state[0].Rule -or $null -eq $state[0].Connector) {
        throw 'The sync server returned incomplete diagnostic state. No anchor was verified.'
    }
    $state[0]
}