src/SQL/Assert-XrmTdsEndpointEnabled.ps1

<#
    .SYNOPSIS
    Check if TDS endpoint is enabled.
    
    .DESCRIPTION
    Assert orgdbsettings EnableTDSEndpoint parameter is true to allow SQL commands thru TDS Endpoint.

    .PARAMETER XrmClient
    Xrm connector initialized to target instance. Use latest one by default. (Dataverse ServiceClient)

    .OUTPUTS
    System.Void. Throws an error when the TDS endpoint is disabled.

    .EXAMPLE
    try {
        Assert-XrmTdsEndpointEnabled -XrmClient $xrmClient;
    }
    catch {
        Enable-XrmTdsEndpoint -XrmClient $xrmClient;
    }

    .LINK
    https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/Assert-XrmTdsEndpointEnabled.md
#>

function Assert-XrmTdsEndpointEnabled {
    [CmdletBinding()]
    [OutputType([System.Void])]
    param
    (
        [Parameter(Mandatory = $false, ValueFromPipeline)]
        [Microsoft.PowerPlatform.Dataverse.Client.ServiceClient]
        $XrmClient = $Global:XrmClient

    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters);        
    }    
    process {            
        $value = Get-XrmOrganizationDbSetting -XrmClient $XrmClient -Name "EnableTDSEndpoint";
        if($value -ne "true")
        {
            throw "TDS EndPoint is disabled! Please use Enable-XrmTdsEndpoint to configure it."
        }
    }
    end {
        $StopWatch.Stop();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Assert-XrmTdsEndpointEnabled -Alias *;