Skip to content

Commit 1f0ec53

Browse files
committed
Invoke-ReportServerSetupAction: Expand environment variables in path parameters
1 parent 82bd423 commit 1f0ec53

File tree

3 files changed

+153
-3
lines changed

3 files changed

+153
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
### Added
99

10+
- `Invoke-ReportServerSetupAction`
11+
- Now uses `Format-Path` with `-ExpandEnvironmentVariable` to expand environment
12+
variables in all path parameters (`MediaPath`, `LogPath`, `InstallFolder`)
13+
([issue #2085](https://github.com/dsccommunity/SqlServerDsc/issues/2085)).
1014
- `Install-SqlDscServer`
1115
- Added parameter `AllowDqRemoval` to the `Upgrade` parameter set
1216
([issue #2155](https://github.com/dsccommunity/SqlServerDsc/issues/2155)).

source/Private/Invoke-ReportServerSetupAction.ps1

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,26 @@ function Invoke-ReportServerSetupAction
242242

243243
$ErrorActionPreference = $previousErrorActionPreference
244244

245+
# Normalize paths: expand environment variables, ensure drive letter root, and remove trailing separators.
246+
$formatPathParameters = @{
247+
EnsureDriveLetterRoot = $true
248+
NoTrailingDirectorySeparator = $true
249+
ExpandEnvironmentVariable = $true
250+
ErrorAction = 'Stop'
251+
}
252+
253+
$MediaPath = Format-Path @formatPathParameters -Path $MediaPath
254+
255+
if ($PSBoundParameters.ContainsKey('LogPath'))
256+
{
257+
$LogPath = Format-Path @formatPathParameters -Path $LogPath
258+
}
259+
260+
if ($PSBoundParameters.ContainsKey('InstallFolder'))
261+
{
262+
$InstallFolder = Format-Path @formatPathParameters -Path $InstallFolder
263+
}
264+
245265
# Sensitive values.
246266
$sensitiveValue = @()
247267

@@ -318,10 +338,8 @@ function Invoke-ReportServerSetupAction
318338

319339
if ($PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage))
320340
{
321-
$expandedMediaPath = [System.Environment]::ExpandEnvironmentVariables($MediaPath)
322-
323341
$startProcessParameters = @{
324-
FilePath = $expandedMediaPath
342+
FilePath = $MediaPath
325343
ArgumentList = $setupArgument
326344
Timeout = $Timeout
327345
}

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

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,134 @@ Describe 'Invoke-ReportServerSetupAction' -Tag 'Private' {
429429
}
430430
}
431431
}
432+
433+
Context 'When path parameters contain environment variables' {
434+
BeforeAll {
435+
# Mock Test-Path to return $true for all paths in this context
436+
Mock -CommandName Test-Path -MockWith {
437+
return $true
438+
}
439+
440+
Mock -CommandName Start-SqlSetupProcess -MockWith {
441+
return 0
442+
} -RemoveParameterValidation 'FilePath'
443+
444+
Mock -CommandName Format-Path -MockWith {
445+
# Return expanded path for testing
446+
return 'C:\Logs\Test\setup.log'
447+
} -ParameterFilter {
448+
$Path -eq '%TEMP%\setup.log'
449+
}
450+
451+
Mock -CommandName Format-Path -MockWith {
452+
return 'C:\Program Files\ReportServer'
453+
} -ParameterFilter {
454+
$Path -eq '%ProgramFiles%\ReportServer'
455+
}
456+
457+
Mock -CommandName Format-Path -MockWith {
458+
return 'C:\SqlMedia\setup.exe'
459+
} -ParameterFilter {
460+
$Path -eq '%SystemDrive%\SqlMedia\setup.exe'
461+
}
462+
463+
InModuleScope -ScriptBlock {
464+
$script:mockDefaultParameters = @{
465+
Install = $true
466+
AcceptLicensingTerms = $true
467+
MediaPath = '%SystemDrive%\SqlMedia\setup.exe'
468+
Force = $true
469+
}
470+
}
471+
}
472+
473+
It 'Should call Format-Path for MediaPath parameter' {
474+
InModuleScope -ScriptBlock {
475+
Invoke-ReportServerSetupAction @mockDefaultParameters
476+
477+
Should -Invoke -CommandName Format-Path -ParameterFilter {
478+
$Path -eq '%SystemDrive%\SqlMedia\setup.exe' -and
479+
$EnsureDriveLetterRoot -eq $true -and
480+
$NoTrailingDirectorySeparator -eq $true -and
481+
$ExpandEnvironmentVariable -eq $true
482+
} -Exactly -Times 1 -Scope It
483+
}
484+
}
485+
486+
It 'Should call Format-Path for LogPath parameter when specified' {
487+
InModuleScope -ScriptBlock {
488+
$installParameters = $mockDefaultParameters.Clone()
489+
$installParameters.LogPath = '%TEMP%\setup.log'
490+
491+
Invoke-ReportServerSetupAction @installParameters
492+
493+
Should -Invoke -CommandName Format-Path -ParameterFilter {
494+
$Path -eq '%TEMP%\setup.log' -and
495+
$EnsureDriveLetterRoot -eq $true -and
496+
$NoTrailingDirectorySeparator -eq $true -and
497+
$ExpandEnvironmentVariable -eq $true
498+
} -Exactly -Times 1 -Scope It
499+
}
500+
}
501+
502+
It 'Should call Format-Path for InstallFolder parameter when specified' {
503+
InModuleScope -ScriptBlock {
504+
$installParameters = $mockDefaultParameters.Clone()
505+
$installParameters.InstallFolder = '%ProgramFiles%\ReportServer'
506+
507+
Invoke-ReportServerSetupAction @installParameters
508+
509+
Should -Invoke -CommandName Format-Path -ParameterFilter {
510+
$Path -eq '%ProgramFiles%\ReportServer' -and
511+
$EnsureDriveLetterRoot -eq $true -and
512+
$NoTrailingDirectorySeparator -eq $true -and
513+
$ExpandEnvironmentVariable -eq $true
514+
} -Exactly -Times 1 -Scope It
515+
}
516+
}
517+
518+
It 'Should pass the expanded path to Start-SqlSetupProcess for MediaPath' {
519+
InModuleScope -ScriptBlock {
520+
Invoke-ReportServerSetupAction @mockDefaultParameters
521+
522+
Should -Invoke -CommandName Start-SqlSetupProcess -ParameterFilter {
523+
$FilePath -eq 'C:\SqlMedia\setup.exe'
524+
} -Exactly -Times 1 -Scope It
525+
}
526+
}
527+
528+
It 'Should pass the expanded path in ArgumentList for LogPath' {
529+
InModuleScope -ScriptBlock {
530+
$installParameters = $mockDefaultParameters.Clone()
531+
$installParameters.LogPath = '%TEMP%\setup.log'
532+
533+
Invoke-ReportServerSetupAction @installParameters
534+
535+
Should -Invoke -CommandName Start-SqlSetupProcess -ParameterFilter {
536+
$ArgumentList | Should -MatchExactly '\/log "C:\\Logs\\Test\\setup\.log"'
537+
538+
# Return $true if none of the above throw.
539+
$true
540+
} -Exactly -Times 1 -Scope It
541+
}
542+
}
543+
544+
It 'Should pass the expanded path in ArgumentList for InstallFolder' {
545+
InModuleScope -ScriptBlock {
546+
$installParameters = $mockDefaultParameters.Clone()
547+
$installParameters.InstallFolder = '%ProgramFiles%\ReportServer'
548+
549+
Invoke-ReportServerSetupAction @installParameters
550+
551+
Should -Invoke -CommandName Start-SqlSetupProcess -ParameterFilter {
552+
$ArgumentList | Should -MatchExactly '\/InstallFolder="C:\\Program Files\\ReportServer"'
553+
554+
# Return $true if none of the above throw.
555+
$true
556+
} -Exactly -Times 1 -Scope It
557+
}
558+
}
559+
}
432560
}
433561

434562
Context 'When setup action is ''Uninstall''' {

0 commit comments

Comments
 (0)