Skip to content

Test-TargetResource throws unhandled exception instead of returning $false (Azure Arc Guest Configuration failure) #2451

@manuelgr0

Description

@manuelgr0

Problem description

I am using Azure Arc Guest Configuration to manage SQL Server settings. This issue appears to be systemic across multiple resources in this module (e.g., SqlConfiguration, SqlDatabaseMail, and others).

When the Guest Configuration agent runs a consistency check, it executes Test-TargetResource. Most resources in this module implement Test- by calling Get-TargetResource, which internally calls Connect-SQL.

If the target SQL Server instance is not yet installed, the service is stopped, or the instance is unreachable, the Connect-SQL helper function throws a terminating error (e.g., SQLCOMMON0019).

Because Test-TargetResource does not catch this exception in many resources, the Guest Configuration agent reports a DscConfigurationExecutionFailed error and marks the resource as non-compliant with a fatal error.

Consequence:
Since the Test method crashes, the Guest Configuration agent never attempts to run Set-TargetResource. This prevents the configuration from ever applying or self-correcting (remediating), even if the policy is set to ApplyAndAutoCorrect.

Expected Behavior:
If Test-TargetResource cannot connect to the SQL instance (e.g., because it doesn't exist yet), it should catch the exception, log a verbose message, and simply return $false. This would allow the Guest Configuration agent to proceed to Set-TargetResource to install or configure the instance.

Actual Behavior:
The resources throw unhandled exceptions, causing the entire Guest Configuration policy application to fail.

Verbose logs

DscConfigurationExecutionFailed

PowerShell DSC resource DSC_SqlConfiguration failed to execute Test-TargetResource functionality with error message: The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Failed to connect to SQL instance 'SQLSERVER\T99'. (SQLCOMMON0019)

DSC configuration

Configuration ReproExample
{
    Import-DscResource -ModuleName SqlServerDsc

    Node localhost
    {
        # This issue affects multiple resources.
        # Example 1: SqlConfiguration
        SqlConfiguration 'ExampleConfig'
        {
            InstanceName = 'MSSQLSERVER'
            OptionName   = 'Max server memory (MB)'
            OptionValue  = 2048
        }

        # Example 2: SqlDatabaseMail
        SqlDatabaseMail 'ExampleMail'
        {
            InstanceName   = 'MSSQLSERVER'
            AccountName    = 'TestAccount'
            EmailAddress   = 'test@contoso.com'
            MailServerName = 'smtp.contoso.com'
            ProfileName    = 'TestProfile'
        }
    }
}

Suggested solution

The Test-TargetResource function in these resources usually calls Get-TargetResource to retrieve the current state. This call should be wrapped in a try...catch block across all affected resources.

If an exception occurs during the "Get" phase (specifically connection errors), Test-TargetResource should swallow the error and return $false.

General fix pattern:

function Test-TargetResource
{
    # ... params ...

    try
    {
        # Attempt to get current state
        $getTargetResourceResult = Get-TargetResource @PSBoundParameters -ErrorAction Stop
    }
    catch
    {
        # If we can't connect, we aren't in the desired state. 
        # Return $false so Set-TargetResource can run.
        Write-Verbose -Message "Test-TargetResource: Unable to connect to SQL instance. Assuming resource is not in desired state. Error: $_"
        return $false
    }

    # ... existing comparison logic ...
}

### SQL Server edition and version

```text
2022

SQL Server PowerShell modules

22.4.5.1

Operating system

Windows Server 2022

PowerShell version

7

SqlServerDsc version

17.5.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs investigationThe issue needs to be investigated by the maintainers or/and the community.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions