Skip to content

Commit 5c65e63

Browse files
authored
Invoke-ReportServerSetupAction: Expand environment variables in path parameters (#2383)
1 parent f8dbaea commit 5c65e63

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
- Added public command `Get-SqlDscServerProtocolTcpIp` to retrieve TCP/IP address
1115
group information for SQL Server instances. Returns `ServerIPAddress` objects
1216
containing port configuration including `TcpPort`, `TcpDynamicPorts`, `Enabled`,

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

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

0 commit comments

Comments
 (0)