Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `SqlRSSetup`
- Re-added `ReportServerEdition` enum and updated class to use enum instead of
ValidateSet for the Edition property.
- Fixed commands continuing execution after `Assert-ElevatedUser` elevation
errors by setting `$ErrorActionPreference = 'Stop'` [issue #2070](https://github.com/dsccommunity/SqlServerDsc/issues/2070)

### Added

Expand Down
2 changes: 2 additions & 0 deletions source/Private/Invoke-ReportServerSetupAction.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ function Invoke-ReportServerSetupAction
$PassThru
)

$ErrorActionPreference = 'Stop'

if ($Force.IsPresent -and -not $Confirm)
{
$ConfirmPreference = 'None'
Expand Down
2 changes: 2 additions & 0 deletions source/Private/Invoke-SetupAction.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,8 @@ function Invoke-SetupAction
$Force
)

$ErrorActionPreference = 'Stop'

if ($Force.IsPresent -and -not $Confirm)
{
$ConfirmPreference = 'None'
Expand Down
2 changes: 2 additions & 0 deletions source/Public/Get-SqlDscStartupParameter.ps1
Comment thread
johlju marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ function Get-SqlDscStartupParameter
$InstanceName = 'MSSQLSERVER'
)

$ErrorActionPreference = 'Stop'

Assert-ElevatedUser -ErrorAction 'Stop'

if ($PSCmdlet.ParameterSetName -eq 'ByServiceObject')
Expand Down
2 changes: 2 additions & 0 deletions source/Public/Set-SqlDscStartupParameter.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ function Set-SqlDscStartupParameter

begin
{
$ErrorActionPreference = 'Stop'

Assert-ElevatedUser -ErrorAction 'Stop'

if ($Force.IsPresent -and -not $Confirm)
Expand Down
40 changes: 40 additions & 0 deletions tests/Unit/Private/Invoke-ReportServerSetupAction.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,46 @@ Describe 'Invoke-ReportServerSetupAction' -Tag 'Private' {
}
}

Context 'When user is not elevated' {
BeforeAll {
# Mock Assert-ElevatedUser to throw the same error it would in a real scenario
Mock -CommandName Assert-ElevatedUser -MockWith {
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
'This command must run in an elevated PowerShell session. (DRC0043)',
'UserNotElevated',
[System.Management.Automation.ErrorCategory]::InvalidOperation,
'Command parameters'
)
)
}

# Create a valid executable file for the test
New-Item -Path "$TestDrive/ssrs.exe" -ItemType File -Force | Out-Null

InModuleScope -ScriptBlock {
$script:mockDefaultParameters = @{
Install = $true
AcceptLicensingTerms = $true
MediaPath = "$TestDrive/ssrs.exe"
Force = $true
}
}
}

It 'Should throw a terminating error and not continue execution' {
InModuleScope -ScriptBlock {
# This test verifies the fix for issue #2070 where Assert-ElevatedUser
# would throw an error but the function would continue executing
{ Invoke-ReportServerSetupAction @mockDefaultParameters } |
Should -Throw -ExpectedMessage '*This command must run in an elevated PowerShell session*'
}

# Ensure Assert-ElevatedUser was called
Should -Invoke -CommandName Assert-ElevatedUser -Exactly -Times 1 -Scope It
}
}

Context 'When passing no existent path to parameter MediaPath' {
BeforeAll {
Mock -CommandName Assert-ElevatedUser
Expand Down
Loading