Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ applyTo: "**/*.ps?(m|d)1"
- Place ShouldProcess check immediately before each state-change
- `$PSCmdlet.ShouldProcess` must use required pattern
- Never use backtick as line continuation in production code.
- Set `$ErrorActionPreference = 'Stop'` before commands using `-ErrorAction 'Stop'`; restore after

## Output streams

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

### Security & Safety

Expand Down
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
6 changes: 6 additions & 0 deletions source/Private/Invoke-ReportServerSetupAction.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ function Invoke-ReportServerSetupAction
$PassThru
)

$previousErrorActionPreference = $ErrorActionPreference

$ErrorActionPreference = 'Stop'

if ($Force.IsPresent -and -not $Confirm)
{
$ConfirmPreference = 'None'
Expand Down Expand Up @@ -226,6 +230,8 @@ function Invoke-ReportServerSetupAction

Assert-BoundParameter @assertBoundParameters

$ErrorActionPreference = $previousErrorActionPreference

# Sensitive values.
$sensitiveValue = @()

Expand Down
6 changes: 6 additions & 0 deletions source/Private/Invoke-SetupAction.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,10 @@ function Invoke-SetupAction
$Force
)

$previousErrorActionPreference = $ErrorActionPreference

$ErrorActionPreference = 'Stop'

if ($Force.IsPresent -and -not $Confirm)
{
$ConfirmPreference = 'None'
Expand Down Expand Up @@ -1411,6 +1415,8 @@ function Invoke-SetupAction

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

$ErrorActionPreference = $previousErrorActionPreference

$setupArgument = '/QUIET /ACTION={0}' -f $setupAction

if ($DebugPreference -in @('Continue', 'Inquire'))
Expand Down
6 changes: 6 additions & 0 deletions source/Public/Get-SqlDscStartupParameter.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,19 @@ function Get-SqlDscStartupParameter
$InstanceName = 'MSSQLSERVER'
)

$previousErrorActionPreference = $ErrorActionPreference

$ErrorActionPreference = 'Stop'

Assert-ElevatedUser -ErrorAction 'Stop'

if ($PSCmdlet.ParameterSetName -eq 'ByServiceObject')
{
$ServiceObject | Assert-ManagedServiceType -ServiceType 'DatabaseEngine'
}

$ErrorActionPreference = $previousErrorActionPreference

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

begin
{
$previousErrorActionPreference = $ErrorActionPreference

$ErrorActionPreference = 'Stop'

Assert-ElevatedUser -ErrorAction 'Stop'

if ($Force.IsPresent -and -not $Confirm)
{
$ConfirmPreference = 'None'
}

$ErrorActionPreference = $previousErrorActionPreference
}

process
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
1 change: 0 additions & 1 deletion tests/Unit/Public/Get-SqlDscStartupParameter.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ Describe 'Get-SqlDscStartupParameter' -Tag 'Public' {
Context 'When passing server name but an Managed Computer Service object is not returned' {
BeforeAll {
Mock -CommandName Assert-ElevatedUser

Mock -CommandName Get-SqlDscManagedComputerService -MockWith {
return $null
}
Expand Down
Loading