Skip to content
Open
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
19 changes: 15 additions & 4 deletions src/dsc/psresourceget.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,11 @@ function SetPSResourceList {
if ($resourcesToUninstall.Count -gt 0) {
Write-Trace -message "Uninstalling resources: $($resourcesToUninstall | ForEach-Object { "$($_.Name) - $($_.Version)" })" -level debug
$resourcesToUninstall | ForEach-Object {
Uninstall-PSResource -Name $_.Name -Scope $scope -ErrorAction Stop
$cmdWarnings = $null
Uninstall-PSResource -Name $_.Name -Scope $scope -ErrorAction Stop -WarningVariable cmdWarnings
foreach ($w in $cmdWarnings) {
Write-Trace -message ([string]$w) -level warn
}
Comment thread
Gijsreyn marked this conversation as resolved.
}
$resourcesChanged = $true
}
Expand Down Expand Up @@ -614,9 +618,12 @@ function SetPSResourceList {
$version = $_.Version

try {
Install-PSResource -Name $_.Name -Version $_.Version -Scope $scope -Repository $repositoryName -ErrorAction Stop -TrustRepository:$inputObj.trustedRepository -Prerelease:$usePrerelease -Reinstall
}
catch {
$cmdWarnings = $null
Install-PSResource -Name $_.Name -Version $_.Version -Scope $scope -Repository $repositoryName -ErrorAction Stop -TrustRepository:$inputObj.trustedRepository -Prerelease:$usePrerelease -Reinstall -WarningVariable cmdWarnings
foreach ($w in $cmdWarnings) {
Write-Trace -message ([string]$w) -level warn
}
} catch {
Write-Trace -level error -message "Failed to install resource '$name' with version '$version'. Error: $($_.Exception.Message)"
$installErrors += $_.Exception.Message
}
Expand Down Expand Up @@ -825,6 +832,10 @@ if ($null -eq (Get-Module -Name Microsoft.PowerShell.PSResourceGet)) {
Import-Module -Name $path -Force -ErrorAction Stop
}

# Suppress warnings from PSResourceGet cmdlets to prevent them from reaching stdout and
# breaking DSC's JSON parsing. Warnings should be captured on individual cmdlets
$WarningPreference = 'SilentlyContinue'
Comment thread
Gijsreyn marked this conversation as resolved.

switch ($Operation.ToLower()) {
'get' { return (GetOperation -ResourceType $ResourceType) }
'set' { return (SetOperation -ResourceType $ResourceType) }
Expand Down
31 changes: 31 additions & 0 deletions test/DscResource/PSResourceGetDSCResource.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,37 @@ Describe "PSResourceList Resource Tests" -Tags 'CI' {
$setResult.afterState.resources[1].version | Should -Be '5.0.0'
}

It 'Set operation stdout contains only valid JSON and is not contaminated by warning messages' {
# Simple regression test as it is hard to predict a warning message but we want to ensure they do not break DSC's JSON parsing. This test does not verify that warnings are emitted when expected,
# only that if they are emitted they do not reach stdout.
Uninstall-PSResource -Name $script:testModuleName -ErrorAction SilentlyContinue

$psResourceListParams = @{
repositoryName = $script:localRepo
trustedRepository = $true
resources = @(
@{
name = $script:testModuleName
version = '1.0.0'
}
)
}

$resourceInput = $psResourceListParams | ConvertTo-Json -Depth 5

# Capture only stdout; stderr carries DSC trace messages and is intentionally discarded
$stdoutLines = & $script:dscExe resource set --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json 2>$null

# No stdout line should contain warning text or ANSI escape sequences
$stdoutLines | Where-Object { $_ } | ForEach-Object {
$_ | Should -Not -Match 'WARNING:'
$_ | Should -Not -Match '\x1b\['
}

# stdout must be parseable as JSON without error
{ $stdoutLines | ConvertFrom-Json -ErrorAction Stop } | Should -Not -Throw
Comment thread
Gijsreyn marked this conversation as resolved.
}

It 'Can test a PSResourceList resource instance with resources' {
$psResourceListParams = @{
repositoryName = $script:localRepo
Expand Down