Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
529759f
Update New-SqlDscDatabase for improved error handling and update test…
johlju Oct 20, 2025
838142e
Update Remove-SqlDscRole for improved error handling using ThrowTermi…
johlju Oct 20, 2025
1f57542
Update Set-SqlDscDatabase for improved error handling using ThrowTerm…
johlju Oct 20, 2025
f8522f1
Update Set-SqlDscAgentAlert for improved error handling using ThrowTe…
johlju Oct 20, 2025
9102fb9
Update Remove-SqlDscDatabase for improved error handling using ThrowT…
johlju Oct 20, 2025
fbc4055
Update Remove-SqlDscAgentAlert for improved error handling using Thro…
johlju Oct 20, 2025
7abc53a
Update Remove-SqlDscRole to improve error handling by using ItemNotFo…
johlju Oct 20, 2025
fa615da
Update Set-SqlDscDatabase to enhance error handling by using specific…
johlju Oct 20, 2025
797cc46
Update SqlServerDsc.strings.psd1 to add error codes for role not foun…
johlju Oct 20, 2025
8d65470
Update Set-SqlDscDatabase to improve error handling by including the …
johlju Oct 20, 2025
0756914
Update error messages in Get-SqlDscRole and Remove-SqlDscRole for con…
johlju Oct 20, 2025
6051b1e
Improve error handling in Get-SqlDscRole by adding return statement a…
johlju Oct 20, 2025
d4b0bf0
Enhance New-SqlDscRole to adjust ConfirmPreference based on Force par…
johlju Oct 20, 2025
06b674d
Adjust ConfirmPreference in Remove-SqlDscDatabase based on Force para…
johlju Oct 20, 2025
743cb5c
Adjust ConfirmPreference handling in Remove-SqlDscRole for Force para…
johlju Oct 20, 2025
107fcd6
Adjust ConfirmPreference handling in Set-SqlDscDatabase for Force par…
johlju Oct 20, 2025
6884f3b
Update error message expectation in Get-SqlDscRole integration test f…
johlju Oct 21, 2025
bf64070
Update error codes in New-SqlDscRole for consistency
johlju Oct 21, 2025
f5bb81b
Refactor ShouldProcess messages in New-SqlDscRole for clarity and con…
johlju Oct 21, 2025
6d3a3de
Refactor ShouldProcess messages in Remove-SqlDscRole for clarity and …
johlju Oct 21, 2025
6c7f034
Refactor Remove-SqlDscDatabase function for improved error handling a…
johlju Oct 21, 2025
33271c9
Refactor error handling in Get-SqlDscRole integration test for non-ex…
johlju Oct 21, 2025
99f3e18
Add DatabaseUserAccess enum for database user access management tests
johlju Oct 21, 2025
b67f844
Refactor error messages in Remove-SqlDscDatabase tests for consistenc…
johlju Oct 21, 2025
8e28bf0
Refactor ShouldProcess messages in Remove-SqlDscDatabase for clarity …
johlju Oct 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- `Remove-SqlDscAgentAlert`
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
functions for proper terminating error handling ([issue #2193](https://github.com/dsccommunity/SqlServerDsc/issues/2193)).
- `Set-SqlDscAgentAlert`
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
functions for proper terminating error handling ([issue #2196](https://github.com/dsccommunity/SqlServerDsc/issues/2196)).
- `Remove-SqlDscDatabase`
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
functions for proper terminating error handling ([issue #2195](https://github.com/dsccommunity/SqlServerDsc/issues/2195)).
- `Remove-SqlDscRole`
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
functions for proper terminating error handling ([issue #2199](https://github.com/dsccommunity/SqlServerDsc/issues/2199)).
- `New-SqlDscDatabase`
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
functions for proper terminating error handling ([issue #2200](https://github.com/dsccommunity/SqlServerDsc/issues/2200)).
- `Set-SqlDscDatabase`
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
functions for proper terminating error handling ([issue #2198](https://github.com/dsccommunity/SqlServerDsc/issues/2198)).
- `Add-SqlDscTraceFlag`
- Improved de-duplication logic to normalize element types to `[System.UInt32]`
before sorting and removing duplicates, ensuring proper handling of mixed
Expand Down
20 changes: 18 additions & 2 deletions source/Public/New-SqlDscDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,15 @@ function New-SqlDscDatabase
if ($CompatibilityLevel -notin $supportedCompatibilityLevels.$($ServerObject.VersionMajor))
{
$errorMessage = $script:localizedData.Database_InvalidCompatibilityLevel -f $CompatibilityLevel, $ServerObject.InstanceName
New-ArgumentException -ArgumentName 'CompatibilityLevel' -Message $errorMessage

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.ArgumentException]::new($errorMessage),
'NSD0003', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidArgument,
$CompatibilityLevel
)
)
}
}

Expand All @@ -157,7 +165,15 @@ function New-SqlDscDatabase
if ($Collation -notin $ServerObject.EnumCollations().Name)
{
$errorMessage = $script:localizedData.Database_InvalidCollation -f $Collation, $ServerObject.InstanceName
New-ArgumentException -ArgumentName 'Collation' -Message $errorMessage

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.ArgumentException]::new($errorMessage),
'NSD0004', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidArgument,
$Collation
)
)
}
}

Expand Down
20 changes: 18 additions & 2 deletions source/Public/Remove-SqlDscAgentAlert.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,13 @@ function Remove-SqlDscAgentAlert

if ($PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage))
{
Write-Verbose -Message ($script:localizedData.Remove_SqlDscAgentAlert_RemovingAlert -f $alertObjectToRemove.Name)

try
{
Write-Verbose -Message ($script:localizedData.Remove_SqlDscAgentAlert_RemovingAlert -f $alertObjectToRemove.Name)
$originalErrorActionPreference = $ErrorActionPreference

$ErrorActionPreference = 'Stop'

$alertObjectToRemove.Drop()

Expand All @@ -128,7 +132,19 @@ function Remove-SqlDscAgentAlert
catch
{
$errorMessage = $script:localizedData.Remove_SqlDscAgentAlert_RemoveFailed -f $alertObjectToRemove.Name
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.InvalidOperationException]::new($errorMessage, $_.Exception),
'RSAA0005', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$alertObjectToRemove
)
)
}
finally
{
$ErrorActionPreference = $originalErrorActionPreference
}
}
}
Expand Down
96 changes: 76 additions & 20 deletions source/Public/Remove-SqlDscDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ function Remove-SqlDscDatabase
if ($Name -in $systemDatabases)
{
$errorMessage = $script:localizedData.Database_CannotRemoveSystem -f $Name
New-InvalidOperationException -Message $errorMessage

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.InvalidOperationException]::new($errorMessage),
'RSDD0001', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$Name
)
)
}

# Get the database object
Expand All @@ -120,7 +128,15 @@ function Remove-SqlDscDatabase
if (-not $DatabaseObject)
{
$errorMessage = $script:localizedData.Database_NotFound -f $Name
New-InvalidOperationException -Message $errorMessage

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.Management.Automation.ItemNotFoundException]::new($errorMessage),
'RSDD0002', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
$Name
)
)
}
}
else
Expand All @@ -133,7 +149,15 @@ function Remove-SqlDscDatabase
if ($Name -in $systemDatabases)
{
$errorMessage = $script:localizedData.Database_CannotRemoveSystem -f $Name
New-InvalidOperationException -Message $errorMessage

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.InvalidOperationException]::new($errorMessage),
'RSDD0003', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$Name
)
)
}
}

Expand All @@ -143,26 +167,46 @@ function Remove-SqlDscDatabase

if ($Force.IsPresent -or $PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage))
{
try
# Drop all active connections if requested
if ($DropConnections.IsPresent)
{
# Drop all active connections if requested
if ($DropConnections.IsPresent)
Write-Verbose -Message ($script:localizedData.Database_DroppingConnections -f $Name)

try
{
Write-Verbose -Message ($script:localizedData.Database_DroppingConnections -f $Name)

try
{
$DatabaseObject.UserAccess = 'Single'
$DatabaseObject.Alter([Microsoft.SqlServer.Management.Smo.TerminationClause]::RollbackTransactionsImmediately)
}
catch
{
$errorMessage = $script:localizedData.Database_DropConnectionsFailed -f $Name
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
$originalErrorActionPreference = $ErrorActionPreference

$ErrorActionPreference = 'Stop'

$DatabaseObject.UserAccess = 'Single'
$DatabaseObject.Alter([Microsoft.SqlServer.Management.Smo.TerminationClause]::RollbackTransactionsImmediately)
}
catch
{
$errorMessage = $script:localizedData.Database_DropConnectionsFailed -f $Name

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.InvalidOperationException]::new($errorMessage, $_.Exception),
'RSDD0004', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$DatabaseObject
)
)
}
finally
{
$ErrorActionPreference = $originalErrorActionPreference
}
}

Write-Verbose -Message ($script:localizedData.Database_Removing -f $Name)

Write-Verbose -Message ($script:localizedData.Database_Removing -f $Name)
try
{
$originalErrorActionPreference = $ErrorActionPreference

$ErrorActionPreference = 'Stop'

$DatabaseObject.Drop()

Expand All @@ -171,7 +215,19 @@ function Remove-SqlDscDatabase
catch
{
$errorMessage = $script:localizedData.Database_RemoveFailed -f $Name, $DatabaseObject.Parent.InstanceName
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.InvalidOperationException]::new($errorMessage, $_.Exception),
'RSDD0005', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$DatabaseObject
)
)
}
finally
{
$ErrorActionPreference = $originalErrorActionPreference
}
}
}
Expand Down
30 changes: 27 additions & 3 deletions source/Public/Remove-SqlDscRole.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,15 @@ function Remove-SqlDscRole
if (-not $RoleObject)
{
$errorMessage = $script:localizedData.Role_NotFound -f $Name
New-InvalidOperationException -Message $errorMessage

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.Management.Automation.ItemNotFoundException]::new($errorMessage),
'RSDR0001', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
$Name
)
)
}
}
else
Expand All @@ -106,7 +114,15 @@ function Remove-SqlDscRole
if ($RoleObject.IsFixedRole)
{
$errorMessage = $script:localizedData.Role_CannotRemoveBuiltIn -f $Name
New-InvalidOperationException -Message $errorMessage

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.InvalidOperationException]::new($errorMessage),
'RSDR0002', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$RoleObject
)
)
}

$verboseDescriptionMessage = $script:localizedData.Role_Remove_ShouldProcessVerboseDescription -f $Name, $RoleObject.Parent.InstanceName
Expand All @@ -126,7 +142,15 @@ function Remove-SqlDscRole
catch
{
$errorMessage = $script:localizedData.Role_RemoveFailed -f $Name, $RoleObject.Parent.InstanceName
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.InvalidOperationException]::new($errorMessage, $_.Exception),
'RSDR0003', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$RoleObject
)
)
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions source/Public/Set-SqlDscAgentAlert.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,15 @@ function Set-SqlDscAgentAlert
if ($null -eq $alertObjectToUpdate)
{
$errorMessage = $script:localizedData.Set_SqlDscAgentAlert_AlertNotFound -f $Name
New-ObjectNotFoundException -Message $errorMessage

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.Management.Automation.ItemNotFoundException]::new($errorMessage),
'SSAA0002', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
$Name
)
)
}
}
else
Expand Down Expand Up @@ -209,7 +217,15 @@ function Set-SqlDscAgentAlert
catch
{
$errorMessage = $script:localizedData.Set_SqlDscAgentAlert_UpdateFailed -f $alertObjectToUpdate.Name
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.InvalidOperationException]::new($errorMessage, $_.Exception),
'SSAA0008', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$alertObjectToUpdate
)
)
}
}
}
Expand Down
40 changes: 36 additions & 4 deletions source/Public/Set-SqlDscDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,15 @@ function Set-SqlDscDatabase
if (-not $DatabaseObject)
{
$errorMessage = $script:localizedData.Database_NotFound -f $Name
New-InvalidOperationException -Message $errorMessage

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.InvalidOperationException]::new($errorMessage),
'SSDD0001', # SQL Server Database - Database not found
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$Name
)
)
}
}
else
Expand Down Expand Up @@ -154,7 +162,15 @@ function Set-SqlDscDatabase
if ($CompatibilityLevel -notin $supportedCompatibilityLevels.$($ServerObject.VersionMajor))
{
$errorMessage = $script:localizedData.Database_InvalidCompatibilityLevel -f $CompatibilityLevel, $ServerObject.InstanceName
New-ObjectNotFoundException -Message $errorMessage

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.ArgumentException]::new($errorMessage),
'SSDD0002', # SQL Server Database - Invalid compatibility level
[System.Management.Automation.ErrorCategory]::InvalidArgument,
$CompatibilityLevel
)
)
}
}

Expand All @@ -164,7 +180,15 @@ function Set-SqlDscDatabase
if ($Collation -notin $ServerObject.EnumCollations().Name)
{
$errorMessage = $script:localizedData.Database_InvalidCollation -f $Collation, $ServerObject.InstanceName
New-ObjectNotFoundException -Message $errorMessage

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.ArgumentException]::new($errorMessage),
'SSDD0003', # SQL Server Database - Invalid collation
[System.Management.Automation.ErrorCategory]::InvalidArgument,
$Collation
)
)
}
}

Expand Down Expand Up @@ -221,7 +245,15 @@ function Set-SqlDscDatabase
catch
{
$errorMessage = $script:localizedData.Database_SetFailed -f $Name, $ServerObject.InstanceName
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_

$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.InvalidOperationException]::new($errorMessage, $_.Exception),
'SSDD0004', # SQL Server Database - Set failed
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$DatabaseObject
)
)
}
}
}
Expand Down
Loading
Loading