internal/functions/tests/Test-DcnConfiguration.ps1

function Test-DcnConfiguration {
    <#
    .SYNOPSIS
        Test the configuration of the module

    .DESCRIPTION
        The configuration of the module is vital to let it function.
        This function checks several configurations

    .PARAMETER SqlInstance
        The instance that represents the dbaclone instance that holds the database

    .PARAMETER SqlCredential
        Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use:

        $scred = Get-Credential, then pass $scred object to the -SourceSqlCredential parameter.

        Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials.
        To connect as a different Windows user, run PowerShell as that user.

    .PARAMETER Database
        The database that holds all the information for the dbaclone module

    .PARAMETER EnableException
        By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
        This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
        Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

    .PARAMETER WhatIf
        If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.

    .PARAMETER Confirm
        If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.

    .NOTES
        Author: Sander Stad (@sqlstad, sqlstad.nl)

        Website: https://dbaclone.org
        Copyright: (C) Sander Stad, sander@sqlstad.nl
        License: MIT https://opensource.org/licenses/MIT

    .LINK
        https://dbaclone.org/

    .EXAMPLE
        Test-DcnConfiguration

        Test the configuration of the module retrieving the set configurations

    .EXAMPLE
        Test-DcnConfiguration -SqlInstance SQLDB1 -Database dbaclone

        Test the configuration with the instance and database set

    #>


    [CmdLetBinding()]

    param(
        [DbaInstanceParameter]$SqlInstance,
        [System.Management.Automation.PSCredential]
        $SqlCredential,
        [string]$Database,
        [switch]$EnableException
    )

    Write-PSFMessage -Message "SqlInstance: $SqlInstance, Database: $Database" -Level Debug

    # Check if the values for the dbaclone database are set
    if (($null -eq $SqlInstance) -or ($null -eq $Database) -or ($null -eq $SqlCredential)) {
        # Get the configurations for the program database
        $Database = Get-PSFConfigValue -FullName dbaclone.database.name -Fallback "NotConfigured"
        $SqlInstance = Get-PSFConfigValue -FullName dbaclone.database.server -Fallback "NotConfigured"
        $SqlCredential = Get-PSFConfigValue -FullName dbaclone.informationstore.credential -Fallback $null
    }

    Write-PSFMessage -Message "Checking configurations" -Level Verbose

    # Check the module database server and database name configurations
    if ($SqlInstance -eq 'NotConfigured') {
        Stop-PSFFunction -Message "The dbaclone database server is not yet configured. Please run Set-DcnConfiguration" -Target $SqlInstance -Continue
    }

    if ($Database -eq 'NotConfigured') {
        Stop-PSFFunction -Message "The dbaclone database is not yet configured. Please run Set-DcnConfiguration" -Target $Database -Continue
    }

    Write-PSFMessage -Message "Attempting to connect to dbaclone database server $SqlInstance.." -Level Verbose
    try {
        $pdcServer = Connect-DbaInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential -NonPooledConnection
    }
    catch {
        Stop-PSFFunction -Message "Could not connect to Sql Server instance $SqlInstance" -ErrorRecord $_ -Target $pdcServer -Continue
    }

    # Check if the dbaclone database is present
    if ($pdcServer.Databases.Name -notcontains $Database) {
        Stop-PSFFunction -Message "dbaclone database $Database is not present on $SqlInstance" -Target $pdcServer -Continue
    }

    Write-PSFMessage -Message "Finished checking configurations" -Level Verbose

}