Skip to content

Commit f908d98

Browse files
authored
Fix Assert-ElevatedUser not terminating correctly (#2125)
1 parent e8aeef5 commit f908d98

File tree

8 files changed

+68
-1
lines changed

8 files changed

+68
-1
lines changed

.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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
- `SqlRSSetup`
1919
- Re-added `ReportServerEdition` enum and updated class to use enum instead of
2020
ValidateSet for the Edition property.
21+
- Fixed commands continuing execution after `Assert-ElevatedUser` elevation
22+
errors by setting `$ErrorActionPreference = 'Stop'` [issue #2070](https://github.com/dsccommunity/SqlServerDsc/issues/2070)
2123

2224
### Added
2325

source/Private/Invoke-ReportServerSetupAction.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ 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'
@@ -226,6 +230,8 @@ function Invoke-ReportServerSetupAction
226230

227231
Assert-BoundParameter @assertBoundParameters
228232

233+
$ErrorActionPreference = $previousErrorActionPreference
234+
229235
# Sensitive values.
230236
$sensitiveValue = @()
231237

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+
$previousErrorActionPreference = $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 = $previousErrorActionPreference
1419+
14141420
$setupArgument = '/QUIET /ACTION={0}' -f $setupAction
14151421

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

source/Public/Get-SqlDscStartupParameter.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,19 @@ function Get-SqlDscStartupParameter
6565
$InstanceName = 'MSSQLSERVER'
6666
)
6767

68+
$previousErrorActionPreference = $ErrorActionPreference
69+
70+
$ErrorActionPreference = 'Stop'
71+
6872
Assert-ElevatedUser -ErrorAction 'Stop'
6973

7074
if ($PSCmdlet.ParameterSetName -eq 'ByServiceObject')
7175
{
7276
$ServiceObject | Assert-ManagedServiceType -ServiceType 'DatabaseEngine'
7377
}
7478

79+
$ErrorActionPreference = $previousErrorActionPreference
80+
7581
if ($PSCmdlet.ParameterSetName -eq 'ByServerName')
7682
{
7783
$getSqlDscManagedComputerServiceParameters = @{

source/Public/Set-SqlDscStartupParameter.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,18 @@ function Set-SqlDscStartupParameter
9191

9292
begin
9393
{
94+
$previousErrorActionPreference = $ErrorActionPreference
95+
96+
$ErrorActionPreference = 'Stop'
97+
9498
Assert-ElevatedUser -ErrorAction 'Stop'
9599

96100
if ($Force.IsPresent -and -not $Confirm)
97101
{
98102
$ConfirmPreference = 'None'
99103
}
104+
105+
$ErrorActionPreference = $previousErrorActionPreference
100106
}
101107

102108
process

tests/Unit/Private/Invoke-ReportServerSetupAction.Tests.ps1

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,46 @@ Describe 'Invoke-ReportServerSetupAction' -Tag 'Private' {
9999
}
100100
}
101101

102+
Context 'When user is not elevated' {
103+
BeforeAll {
104+
# Mock Assert-ElevatedUser to throw the same error it would in a real scenario
105+
Mock -CommandName Assert-ElevatedUser -MockWith {
106+
$PSCmdlet.ThrowTerminatingError(
107+
[System.Management.Automation.ErrorRecord]::new(
108+
'This command must run in an elevated PowerShell session. (DRC0043)',
109+
'UserNotElevated',
110+
[System.Management.Automation.ErrorCategory]::InvalidOperation,
111+
'Command parameters'
112+
)
113+
)
114+
}
115+
116+
# Create a valid executable file for the test
117+
New-Item -Path "$TestDrive/ssrs.exe" -ItemType File -Force | Out-Null
118+
119+
InModuleScope -ScriptBlock {
120+
$script:mockDefaultParameters = @{
121+
Install = $true
122+
AcceptLicensingTerms = $true
123+
MediaPath = "$TestDrive/ssrs.exe"
124+
Force = $true
125+
}
126+
}
127+
}
128+
129+
It 'Should throw a terminating error and not continue execution' {
130+
InModuleScope -ScriptBlock {
131+
# This test verifies the fix for issue #2070 where Assert-ElevatedUser
132+
# would throw an error but the function would continue executing
133+
{ Invoke-ReportServerSetupAction @mockDefaultParameters } |
134+
Should -Throw -ExpectedMessage '*This command must run in an elevated PowerShell session*'
135+
}
136+
137+
# Ensure Assert-ElevatedUser was called
138+
Should -Invoke -CommandName Assert-ElevatedUser -Exactly -Times 1 -Scope It
139+
}
140+
}
141+
102142
Context 'When passing no existent path to parameter MediaPath' {
103143
BeforeAll {
104144
Mock -CommandName Assert-ElevatedUser

tests/Unit/Public/Get-SqlDscStartupParameter.Tests.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ Describe 'Get-SqlDscStartupParameter' -Tag 'Public' {
125125
Context 'When passing server name but an Managed Computer Service object is not returned' {
126126
BeforeAll {
127127
Mock -CommandName Assert-ElevatedUser
128-
129128
Mock -CommandName Get-SqlDscManagedComputerService -MockWith {
130129
return $null
131130
}

0 commit comments

Comments
 (0)