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
Operating system
PowerShell version
SqlServerDsc version
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 implementTest-by callingGet-TargetResource, which internally callsConnect-SQL.If the target SQL Server instance is not yet installed, the service is stopped, or the instance is unreachable, the
Connect-SQLhelper function throws a terminating error (e.g.,SQLCOMMON0019).Because
Test-TargetResourcedoes not catch this exception in many resources, the Guest Configuration agent reports aDscConfigurationExecutionFailederror and marks the resource as non-compliant with a fatal error.Consequence:
Since the
Testmethod crashes, the Guest Configuration agent never attempts to runSet-TargetResource. This prevents the configuration from ever applying or self-correcting (remediating), even if the policy is set toApplyAndAutoCorrect.Expected Behavior:
If
Test-TargetResourcecannot 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 toSet-TargetResourceto install or configure the instance.Actual Behavior:
The resources throw unhandled exceptions, causing the entire Guest Configuration policy application to fail.
Verbose logs
DSC configuration
Suggested solution
The
Test-TargetResourcefunction in these resources usually callsGet-TargetResourceto retrieve the current state. This call should be wrapped in atry...catchblock across all affected resources.If an exception occurs during the "Get" phase (specifically connection errors),
Test-TargetResourceshould swallow the error and return$false.General fix pattern:
SQL Server PowerShell modules
Operating system
PowerShell version
SqlServerDsc version