Skip to content
Closed
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed `Test-TargetResource` to return `$false` (instead of throwing) when
the SQL script file is missing, enabling `DependsOn` scenarios where the file
is created at runtime.
- SqlAgentAlert, SqlAudit, SqlDatabase, SqlDatabasePermission, SqlPermission,
SqlRSSetup
- Fixed `Test()` method to return `$false` (instead of throwing) when the
SQL Server instance is not reachable or an error occurs, enabling
graceful handling and better support for `DependsOn` scenarios.
- SqlAG, SqlAGDatabase, SqlAGListener, SqlAGReplica, SqlAgentFailsafe,
SqlAgentOperator, SqlAlias, SqlAlwaysOnService, SqlConfiguration,
SqlDatabaseDefaultLocation, SqlDatabaseMail, SqlDatabaseObjectPermission,
SqlDatabaseRole, SqlDatabaseUser, SqlEndpoint, SqlEndpointPermission,
SqlLogin, SqlMaxDop, SqlMemory, SqlProtocol, SqlProtocolTcpIp, SqlRS,
SqlReplication, SqlRole, SqlScript, SqlScriptQuery, SqlSecureConnection,
SqlServiceAccount, SqlSetup, SqlTraceFlag, SqlWaitForAG, SqlWindowsFirewall
- Fixed `Test-TargetResource` to return `$false` (instead of throwing)
when the SQL Server instance is not reachable or an error occurs during
`Get-TargetResource`, enabling graceful handling and better support for
`DependsOn` scenarios.

### Changed

Expand Down
18 changes: 13 additions & 5 deletions source/Classes/020.SqlAgentAlert.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,15 @@ class SqlAgentAlert : SqlResourceBase
[System.Boolean] Test()
{
# Call base implementation to test current state
return ([ResourceBase] $this).Test()
try
{
return ([ResourceBase] $this).Test()
}
catch
{
Write-Verbose -Message ($this.localizedData.SQLInstanceNotReachable -f $_)
return $false
}
}

[void] Set()
Expand All @@ -179,10 +187,10 @@ class SqlAgentAlert : SqlResourceBase

# Validate that both Severity and MessageId are not specified
$assertMutuallyExclusiveParams = @{
BoundParameterList = $properties
MutuallyExclusiveList1 = @('Severity')
MutuallyExclusiveList2 = @('MessageId')
IfEqualParameterList = @{
BoundParameterList = $properties
MutuallyExclusiveList1 = @('Severity')
MutuallyExclusiveList2 = @('MessageId')
IfEqualParameterList = @{
Ensure = 'Present'
}
}
Expand Down
10 changes: 9 additions & 1 deletion source/Classes/020.SqlAudit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,15 @@ class SqlAudit : SqlResourceBase
[System.Boolean] Test()
{
# Call the base method to test all of the properties that should be enforced.
return ([ResourceBase] $this).Test()
try
{
return ([ResourceBase] $this).Test()
}
catch
{
Write-Verbose -Message ($this.localizedData.SQLInstanceNotReachable -f $_)
return $false
}
}

[void] Set()
Expand Down
10 changes: 9 additions & 1 deletion source/Classes/020.SqlDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,15 @@ class SqlDatabase : SqlResourceBase
[System.Boolean] Test()
{
# Call the base method to test all of the properties that should be enforced.
return ([ResourceBase] $this).Test()
try
{
return ([ResourceBase] $this).Test()
}
catch
{
Write-Verbose -Message ($this.localizedData.SQLInstanceNotReachable -f $_)
return $false
}
}

[void] Set()
Expand Down
10 changes: 9 additions & 1 deletion source/Classes/020.SqlDatabasePermission.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,15 @@ class SqlDatabasePermission : SqlResourceBase
[System.Boolean] Test()
{
# Call the base method to test all of the properties that should be enforced.
return ([ResourceBase] $this).Test()
try
{
return ([ResourceBase] $this).Test()
}
catch
{
Write-Verbose -Message ($this.localizedData.SQLInstanceNotReachable -f $_)
return $false
}
}

[void] Set()
Expand Down
11 changes: 9 additions & 2 deletions source/Classes/020.SqlPermission.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,15 @@ class SqlPermission : SqlResourceBase

[System.Boolean] Test()
{
# Call the base method to test all of the properties that should be enforced.
return ([ResourceBase] $this).Test()
try
{
return ([ResourceBase] $this).Test()
}
catch
{
Write-Verbose -Message ($this.localizedData.SQLInstanceNotReachable -f $_)
return $false
}
}

[void] Set()
Expand Down
10 changes: 9 additions & 1 deletion source/Classes/020.SqlRSSetup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,15 @@ class SqlRSSetup : ResourceBase
[System.Boolean] Test()
{
# Call the base method to test all of the properties that should be enforced.
$baseTestResult = ([ResourceBase] $this).Test()
try
{
$baseTestResult = ([ResourceBase] $this).Test()
}
catch
{
Write-Verbose -Message ($this.localizedData.SQLInstanceNotReachable -f $_)
$baseTestResult = $false
}

# If $baseTestResult -eq $true, then the InstanceName exists.
# If $baseTestResult -eq $false, then the InstanceName does not exist.
Expand Down
13 changes: 12 additions & 1 deletion source/DSCResources/DSC_SqlAG/DSC_SqlAG.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,18 @@ function Test-TargetResource
# Assume this will pass. We will determine otherwise later
$result = $true

$getTargetResourceResult = Get-TargetResource @getTargetResourceParameters
try
{
$getTargetResourceResult = Get-TargetResource @getTargetResourceParameters
}
catch
{
Write-Verbose -Message (
$script:localizedData.SQLInstanceNotReachable `
-f $_
)
return $false
}

<#
If this is supposed to process only the active node, and this is not the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ConvertFrom-StringData @'
GetAvailabilityGroup = Get the current configuration for the availability group '{0}' on the instance '{1}'.
SQLInstanceNotReachable = Unable to connect to SQL instance or retrieve option. Assuming resource is not in desired state. Error: {0}
RemoveAvailabilityGroup = Removing the availability group '{0}' on the instance '{1}'.
HadrNotEnabled = Always On Availability Groups is not enabled.
FailedRemoveAvailabilityGroup = Failed to remove the availability group '{0}' from the instance '{1}'.
Expand Down
13 changes: 12 additions & 1 deletion source/DSCResources/DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,18 @@ function Test-TargetResource
AvailabilityGroupName = $AvailabilityGroupName
BackupPath = $BackupPath
}
$currentConfiguration = Get-TargetResource @getTargetResourceParameters
try
{
$currentConfiguration = Get-TargetResource @getTargetResourceParameters
}
catch
{
Write-Verbose -Message (
$script:localizedData.SQLInstanceNotReachable `
-f $_
)
return $false
}

<#
If this is supposed to process only the active node, and this is not the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ConvertFrom-StringData @'
AddingDatabasesToAvailabilityGroup = Adding the following databases to the '{0}' availability group: {1}.
AlterAvailabilityGroupDatabaseMembershipFailure = {0}.
AvailabilityGroupDoesNotExist = The availability group '{0}' does not exist.
SQLInstanceNotReachable = Unable to connect to SQL instance or retrieve option. Assuming resource is not in desired state. Error: {0}
DatabaseShouldBeMember = The following databases should be a member of the availability group '{0}': {1}.
DatabaseShouldNotBeMember = The following databases should not be a member of the availability group '{0}': {1}.
DatabasesNotFound = The following databases were not found in the instance: {0}.
Expand Down
12 changes: 11 additions & 1 deletion source/DSCResources/DSC_SqlAGListener/DSC_SqlAGListener.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,17 @@ function Test-TargetResource
$script:localizedData.TestingConfiguration -f $Name, $AvailabilityGroup, $InstanceName
)

$availabilityGroupListenerState = Get-TargetResource @parameters
try
{
$availabilityGroupListenerState = Get-TargetResource @parameters
}
catch
{
Write-Verbose -Message (
$script:localizedData.SQLInstanceNotReachable -f $_
)
return $false
}

[System.Boolean] $result = $false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ConvertFrom-StringData @'
AvailabilityGroupListenerIsNotPresent = The Availability Group listener '{0}' does not exist.
AvailabilityGroupListenerNotFound = Trying to make a change to the listener '{0}' that does not exist in the availability group '{1}'.
CreateAvailabilityGroupListener = Create Availability Group listener '{0}' for the Availability Group '{1}' on the instance '{2}'.
SQLInstanceNotReachable = Unable to connect to SQL instance or retrieve option. Assuming resource is not in desired state. Error: {0}
SetAvailabilityGroupListenerPort = Availability Group listener port is set to '{0}'.
SetAvailabilityGroupListenerDhcp = Availability Group listener is using DHCP with the subnet '{0}'.
SetAvailabilityGroupListenerDhcpDefaultSubnet = Availability Group listener is using DHCP with the server default subnet.
Expand Down
13 changes: 12 additions & 1 deletion source/DSCResources/DSC_SqlAGReplica/DSC_SqlAGReplica.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,18 @@ function Test-TargetResource
# Assume this will pass. We will determine otherwise later
$result = $true

$getTargetResourceResult = Get-TargetResource @getTargetResourceParameters
try
{
$getTargetResourceResult = Get-TargetResource @getTargetResourceParameters
}
catch
{
Write-Verbose -Message (
$script:localizedData.SQLInstanceNotReachable `
-f $_
)
return $false
}

<#
If this is supposed to process only the active node, and this is not the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ ConvertFrom-StringData @'
GetAvailabilityGroup = Get the current configuration for the availability group replica '{0}' in the availability group '{1}' on the instance '{2}'.
HadrNotEnabled = Always On Availability Groups is not enabled.
FailedRemoveAvailabilityGroupReplica = Failed to remove the availability group replica '{0}' from the availability group '{1}' on the instance '{2}'.
SQLInstanceNotReachable = Unable to connect to SQL instance or retrieve option. Assuming resource is not in desired state. Error: {0}
DatabaseMirroringEndpointNotFound = No database mirroring endpoint was found on '{0}'.
ReplicaNotFound = Unable to find the availability group replica '{0}' in the availability group '{1}' on the instance '{2}'.
FailedCreateAvailabilityGroupReplica = Failed to creating the availability group replica '{0}' for the availability group '{1}' on the instance '{2}'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,18 @@ function Test-TargetResource

$returnValue = $false

$getTargetResourceResult = Get-TargetResource @getTargetResourceParameters
try
{
$getTargetResourceResult = Get-TargetResource @getTargetResourceParameters
}
catch
{
Write-Verbose -Message (
$script:localizedData.SQLInstanceNotReachable `
-f $_
)
return $false
}

Write-Verbose -Message (
$script:localizedData.TestingConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ ConvertFrom-StringData @'
UpdateFailsafeOperatorError = Unable to update Sql Agent Failsafe Operator '{0}' on {1}\\{2}.
RemoveFailsafeOperator = Removing Sql Agent Failsafe Operator.
ConnectServerFailed = Unable to connect to {0}\\{1}.
SQLInstanceNotReachable = Unable to connect to SQL instance or retrieve option. Assuming resource is not in desired state. Error: {0}
TestingConfiguration = Determines if the SQL Agent Failsafe Operator is in the desired state.
'@
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,18 @@ function Test-TargetResource
InstanceName = $InstanceName
}

$getTargetResourceResult = Get-TargetResource @getTargetResourceParameters
try
{
$getTargetResourceResult = Get-TargetResource @getTargetResourceParameters
}
catch
{
Write-Verbose -Message (
$script:localizedData.SQLInstanceNotReachable `
-f $_
)
return $false
}

$isOperatorInDesiredState = $true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ ConvertFrom-StringData @'
SqlAgentOperatorDoesNotExistButShould = SQL Agent Operator does not exist but Ensure is set to Present. The SQL Agent Operator '{0}' should be created.
SqlAgentOperatorExistsButEmailWrong = SQL Agent Operator '{0}' exists but has the wrong email address. Email address is currently '{1}' and should be updated to '{2}'.
ConnectServerFailed = Unable to connect to {0}\\{1}.
SQLInstanceNotReachable = Unable to connect to SQL instance or retrieve option. Assuming resource is not in desired state. Error: {0}
'@
14 changes: 12 additions & 2 deletions source/DSCResources/DSC_SqlAlias/DSC_SqlAlias.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,18 @@ function Test-TargetResource
Name = $PSBoundParameters.Name
}

$currentValues = Get-TargetResource @parameters
try
{
$currentValues = Get-TargetResource @parameters
}
catch
{
Write-Verbose -Message (
$script:localizedData.SQLInstanceNotReachable `
-f $_
)
return $false
}

if ($Ensure -eq $currentValues.Ensure)
{
Expand Down Expand Up @@ -365,6 +376,5 @@ function Test-TargetResource
$script:localizedData.NotInDesiredState -f $Name
)
}

return $result
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ ConvertFrom-StringData @'
RemoveClientAlias64Bit = Removing the SQL Server Client Alias '{0}' (64-bit).
RemoveClientAlias32Bit = Removing the SQL Server Client Alias '{0}' (32-bit).
TestingConfiguration = Determines if the SQL Server Client Alias is in desired state.
ClientAliasMissing = The SQL Server Client Alias '{0}' does not exist.
SQLInstanceNotReachable = Unable to connect to SQL instance or retrieve option. Assuming resource is not in desired state. Error: {0}
ClientAliasMissing = The SQL Server Client Alias '{0}' is missing.
ClientAliasPresent = The SQL Server Client Alias '{0}' exist, verifying values.
InDesiredState = The SQL Server Client Alias '{0}' is in desired state.
NotInDesiredState = The SQL Server Client Alias '{0}' is not in desired state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,18 @@ function Test-TargetResource
InstanceName = $InstanceName
}

$state = Get-TargetResource @getTargetResourceParameters
try
{
$state = Get-TargetResource @getTargetResourceParameters
}
catch
{
Write-Verbose -Message (
$script:localizedData.SQLInstanceNotReachable `
-f $_
)
return $false
}

$isInDesiredState = $true

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ConvertFrom-StringData @'
GetAlwaysOnServiceState = Always On Availability Groups is {0} on the instance '{1}\\{2}'.
SQLInstanceNotReachable = Unable to connect to SQL instance or retrieve option. Assuming resource is not in desired state. Error: {0}
DisableAlwaysOnAvailabilityGroup = Disabling Always On Availability Groups for the instance '{0}\\{1}'.
EnableAlwaysOnAvailabilityGroup = Enabling Always On Availability Groups for the instance '{0}\\{1}'.
RestartingService = Always On Availability Groups has been {0} on the instance '{1}\\{2}'. Restarting the service.
Expand Down
Loading
Loading