Skip to content

Commit f335027

Browse files
authored
Merge branch 'main' into fix/get-sqldscserverpermission
2 parents af44035 + 755ad31 commit f335027

33 files changed

Lines changed: 2841 additions & 30 deletions

.github/instructions/dsc-community-style-guidelines-pester.instructions.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ applyTo: "**/*.[Tt]ests.ps1"
1414
- Never test verbose messages, debug messages or parameter binding behavior
1515
- Pass all mandatory parameters to avoid prompts
1616

17+
## Requirements
18+
- Inside `It` blocks, assign unused return objects to `$null` (unless part of pipeline)
19+
- Tested entity must be called from within the `It` blocks
20+
- Keep results and assertions in same `It` block
21+
- Avoid try-catch-finally for cleanup, use `AfterAll` or `AfterEach`
22+
- Avoid unnecessary remove/recreate cycles
23+
1724
## Naming
1825
- One `Describe` block per file matching the tested entity name
1926
- `Context` descriptions start with 'When'
@@ -51,10 +58,6 @@ applyTo: "**/*.[Tt]ests.ps1"
5158
- Keep scope close to usage context
5259

5360
## Best Practices
54-
- Inside `It` blocks, assign unused return objects to `$null` (unless part of pipeline)
55-
- Tested entity must be called from within the `It` blocks
56-
- Keep results and assertions in same `It` block
5761
- Cover all scenarios and code paths
5862
- Use `BeforeEach` and `AfterEach` sparingly
59-
- Avoid try-catch-finally for cleanup, use `AfterAll` or `AfterEach`
60-
- Avoid unnecessary remove/recreate cycles
63+
- Use `$PSDefaultParameterValues` only for Pester commands (`Describe`, `Context`, `It`, `Mock`, `Should`, `InModuleScope`)

.github/instructions/dsc-community-style-guidelines-powershell.instructions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ applyTo: "**/*.ps?(m|d)1"
8080
- Place ShouldProcess check immediately before each state-change
8181
- `$PSCmdlet.ShouldProcess` must use required pattern
8282
- Never use backtick as line continuation in production code.
83+
- Set `$ErrorActionPreference = 'Stop'` before commands using `-ErrorAction 'Stop'`; restore after
8384

8485
## Output streams
8586

@@ -181,6 +182,7 @@ function Get-Something
181182
- Limit piping to one pipe per line
182183
- Assign function results to variables rather than inline calls
183184
- Return a single, consistent object type per function
185+
- return `$null` for no objects/non-terminating errors
184186

185187
### Security & Safety
186188

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Fixed Azure DevOps pipeline conditions that were preventing DSC resource
1313
integration tests from running when they should by removing incorrect quotes
1414
around boolean values.
15+
- Refactored error handling by removing global `$ErrorActionPreference = 'Stop'`
16+
from 64 PowerShell files and implementing targeted error control for specific
17+
command calls that use `-ErrorAction 'Stop'`.
1518
- `SqlAgentAlert`
1619
- Minor fix in `source/Classes/020.SqlAgentAlert.ps1` to correct `ExcludeDscProperties`
1720
formatting (added missing delimiter).
21+
- `SqlRSSetup`
22+
- Re-added `ReportServerEdition` enum and updated class to use enum instead of
23+
ValidateSet for the Edition property.
24+
- Fixed commands continuing execution after `Assert-ElevatedUser` elevation
25+
errors by setting `$ErrorActionPreference = 'Stop'` [issue #2070](https://github.com/dsccommunity/SqlServerDsc/issues/2070)
1826

1927
### Added
2028

@@ -27,6 +35,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2735
- Enhanced workflow with proper environment variable configuration and DSCv3 verification.
2836
- Fixed environment variable persistence by using $GITHUB_ENV instead of
2937
job-level env declaration.
38+
- `Grant-SqlDscServerPermission`
39+
- Added new public command to grant server permissions to a principal (Login or ServerRole) on a SQL Server Database Engine instance.
40+
- `Deny-SqlDscServerPermission`
41+
- Added new public command to deny server permissions to a principal (Login or ServerRole).
42+
- `Revoke-SqlDscServerPermission`
43+
- Added new public command to revoke server permissions from a principal (Login or ServerRole).
44+
- `Test-SqlDscServerPermission`
45+
- Added new public command with Grant/Deny parameter sets (and `-WithGrant`) to test server permissions for a principal.
3046
- `Assert-SqlDscLogin`
3147
- Added new public command to validate that a specified SQL Server principal
3248
is a login.

azure-pipelines.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,19 +299,24 @@ stages:
299299
'tests/Integration/Commands/Test-SqlDscIsLoginEnabled.Integration.Tests.ps1'
300300
'tests/Integration/Commands/New-SqlDscRole.Integration.Tests.ps1'
301301
'tests/Integration/Commands/Get-SqlDscRole.Integration.Tests.ps1'
302-
'tests/Integration/Commands/Remove-SqlDscRole.Integration.Tests.ps1'
303-
'tests/Integration/Commands/Remove-SqlDscLogin.Integration.Tests.ps1'
302+
'tests/Integration/Commands/Grant-SqlDscServerPermission.Integration.Tests.ps1'
304303
'tests/Integration/Commands/Get-SqlDscServerPermission.Integration.Tests.ps1'
304+
'tests/Integration/Commands/Test-SqlDscServerPermission.Integration.Tests.ps1'
305+
'tests/Integration/Commands/Deny-SqlDscServerPermission.Integration.Tests.ps1'
306+
'tests/Integration/Commands/Revoke-SqlDscServerPermission.Integration.Tests.ps1'
305307
'tests/Integration/Commands/Get-SqlDscDatabase.Integration.Tests.ps1'
306308
'tests/Integration/Commands/New-SqlDscDatabase.Integration.Tests.ps1'
307309
'tests/Integration/Commands/Set-SqlDscDatabase.Integration.Tests.ps1'
308310
'tests/Integration/Commands/Test-SqlDscDatabase.Integration.Tests.ps1'
309-
'tests/Integration/Commands/Remove-SqlDscDatabase.Integration.Tests.ps1'
310311
'tests/Integration/Commands/Get-SqlDscAgentAlert.Integration.Tests.ps1'
311312
'tests/Integration/Commands/New-SqlDscAgentAlert.Integration.Tests.ps1'
312313
'tests/Integration/Commands/Set-SqlDscAgentAlert.Integration.Tests.ps1'
313314
'tests/Integration/Commands/Test-SqlDscAgentAlert.Integration.Tests.ps1'
315+
# Group 8
314316
'tests/Integration/Commands/Remove-SqlDscAgentAlert.Integration.Tests.ps1'
317+
'tests/Integration/Commands/Remove-SqlDscDatabase.Integration.Tests.ps1'
318+
'tests/Integration/Commands/Remove-SqlDscRole.Integration.Tests.ps1'
319+
'tests/Integration/Commands/Remove-SqlDscLogin.Integration.Tests.ps1'
315320
# Group 9
316321
'tests/Integration/Commands/Uninstall-SqlDscServer.Integration.Tests.ps1'
317322
)

source/Classes/020.SqlRSSetup.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ class SqlRSSetup : ResourceBase
187187
$EditionUpgrade
188188

189189
[DscProperty()]
190-
[ValidateSet('Developer', 'Evaluation', 'ExpressAdvanced')]
191-
[System.String]
190+
[ReportServerEdition]
192191
$Edition
193192

194193
[DscProperty()]
@@ -402,8 +401,9 @@ class SqlRSSetup : ResourceBase
402401
hidden [void] Modify([System.Collections.Hashtable] $properties)
403402
{
404403
$getDscPropertyParameters = @{
405-
HasValue = $true
406-
Attribute = @(
404+
HasValue = $true
405+
IgnoreZeroEnumValue = $true
406+
Attribute = @(
407407
'Optional'
408408
'Mandatory'
409409
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<#
2+
.SYNOPSIS
3+
The possible states for the commands and DSC resources that handles
4+
SQL Server Reporting Services or Power BI Report Server and uses the
5+
parameter Edition.
6+
#>
7+
enum ReportServerEdition
8+
{
9+
Developer = 1
10+
Evaluation
11+
ExpressAdvanced
12+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<#
2+
.SYNOPSIS
3+
The possible server permissions that can be granted, denied, or revoked.
4+
5+
.NOTES
6+
The available permissions can be seen in the ServerPermission Class documentation:
7+
https://learn.microsoft.com/en-us/dotnet/api/microsoft.sqlserver.management.smo.serverpermission
8+
#>
9+
enum SqlServerPermission
10+
{
11+
# cSpell:ignore securables
12+
AdministerBulkOperations = 1
13+
AlterAnyAvailabilityGroup
14+
AlterAnyConnection
15+
AlterAnyCredential
16+
AlterAnyDatabase
17+
AlterAnyEndpoint
18+
AlterAnyEventNotification
19+
AlterAnyEventSession
20+
AlterAnyEventSessionAddEvent
21+
AlterAnyEventSessionAddTarget
22+
AlterAnyEventSessionDisable
23+
AlterAnyEventSessionDropEvent
24+
AlterAnyEventSessionDropTarget
25+
AlterAnyEventSessionEnable
26+
AlterAnyEventSessionOption
27+
AlterAnyLinkedServer
28+
AlterAnyLogin
29+
AlterAnyServerAudit
30+
AlterAnyServerRole
31+
AlterResources
32+
AlterServerState
33+
AlterSettings
34+
AlterTrace
35+
AuthenticateServer
36+
ConnectAnyDatabase
37+
ConnectSql
38+
ControlServer
39+
CreateAnyDatabase
40+
CreateAnyEventSession
41+
CreateAvailabilityGroup
42+
CreateDdlEventNotification
43+
CreateEndpoint
44+
CreateLogin
45+
CreateServerRole
46+
CreateTraceEventNotification
47+
DropAnyEventSession
48+
ExternalAccessAssembly
49+
ImpersonateAnyLogin
50+
SelectAllUserSecurables
51+
Shutdown
52+
UnsafeAssembly
53+
ViewAnyCryptographicallySecuredDefinition
54+
ViewAnyDatabase
55+
ViewAnyDefinition
56+
ViewAnyErrorLog
57+
ViewAnyPerformanceDefinition
58+
ViewAnySecurityDefinition
59+
ViewServerPerformanceState
60+
ViewServerSecurityAudit
61+
ViewServerSecurityState
62+
ViewServerState
63+
}

source/Private/Get-FileVersionInformation.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@ function Get-FileVersionInformation
3434

3535
process
3636
{
37+
$originalErrorActionPreference = $ErrorActionPreference
38+
39+
$ErrorActionPreference = 'Stop'
40+
3741
$file = Get-Item -Path $FilePath -ErrorAction 'Stop'
3842

43+
$ErrorActionPreference = $originalErrorActionPreference
44+
3945
if ($file.PSIsContainer)
4046
{
4147
$PSCmdlet.ThrowTerminatingError(

source/Private/Invoke-ReportServerSetupAction.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,23 @@ function Invoke-ReportServerSetupAction
196196
$PassThru
197197
)
198198

199+
$previousErrorActionPreference = $ErrorActionPreference
200+
201+
$ErrorActionPreference = 'Stop'
202+
199203
if ($Force.IsPresent -and -not $Confirm)
200204
{
201205
$ConfirmPreference = 'None'
202206
}
203207

208+
$originalErrorActionPreference = $ErrorActionPreference
209+
210+
$ErrorActionPreference = 'Stop'
211+
204212
Assert-ElevatedUser -ErrorAction 'Stop'
205213

214+
$ErrorActionPreference = $originalErrorActionPreference
215+
206216
$assertBoundParameters = @{
207217
BoundParameterList = $PSBoundParameters
208218
MutuallyExclusiveList1 = @(
@@ -226,6 +236,8 @@ function Invoke-ReportServerSetupAction
226236

227237
Assert-BoundParameter @assertBoundParameters
228238

239+
$ErrorActionPreference = $previousErrorActionPreference
240+
229241
# Sensitive values.
230242
$sensitiveValue = @()
231243

source/Private/Invoke-SetupAction.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,10 @@ function Invoke-SetupAction
13721372
$Force
13731373
)
13741374

1375+
$originalErrorActionPreference = $ErrorActionPreference
1376+
1377+
$ErrorActionPreference = 'Stop'
1378+
13751379
if ($Force.IsPresent -and -not $Confirm)
13761380
{
13771381
$ConfirmPreference = 'None'
@@ -1411,6 +1415,8 @@ function Invoke-SetupAction
14111415

14121416
Assert-SetupActionProperties -Property $PSBoundParameters -SetupAction $setupAction -ErrorAction 'Stop'
14131417

1418+
$ErrorActionPreference = $originalErrorActionPreference
1419+
14141420
$setupArgument = '/QUIET /ACTION={0}' -f $setupAction
14151421

14161422
if ($DebugPreference -in @('Continue', 'Inquire'))

0 commit comments

Comments
 (0)