diff --git a/powershell-adapter/Tests/powershellgroup.config.tests.ps1 b/powershell-adapter/Tests/powershellgroup.config.tests.ps1 index f51f32c1f..cbeb33c37 100644 --- a/powershell-adapter/Tests/powershellgroup.config.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.config.tests.ps1 @@ -33,6 +33,23 @@ Describe 'PowerShell adapter resource tests' { $res.results[0].result.actualState.result[0].properties.EnumProp | Should -BeExactly 'Expected' } + It 'Get does not work on config when module does not exist' { + + $yaml = @' + $schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json + resources: + - name: Working with class-based resources + type: Microsoft.DSC/PowerShell + properties: + resources: + - name: Class-resource Info + type: TestClassResourceNotExist/TestClassResourceNotExist +'@ + $yaml | dsc -l trace config get -f - 2> "$TestDrive/tracing.txt" + $LASTEXITCODE | Should -Be 2 + "$TestDrive/tracing.txt" | Should -FileContentMatch 'DSC resource TestClassResourceNotExist/TestClassResourceNotExist module not found.' + } + It 'Test works on config with class-based resources' { $r = Get-Content -Raw $pwshConfigPath | dsc config test -f - diff --git a/powershell-adapter/psDscAdapter/powershell.resource.ps1 b/powershell-adapter/psDscAdapter/powershell.resource.ps1 index acaafad5e..d0cd3ce7f 100644 --- a/powershell-adapter/psDscAdapter/powershell.resource.ps1 +++ b/powershell-adapter/psDscAdapter/powershell.resource.ps1 @@ -148,11 +148,18 @@ switch ($Operation) { exit 1 } + # get unique module names from the desiredState input + $moduleInput = $desiredState | Select-Object -ExpandProperty Type | Sort-Object -Unique + + # refresh the cache with the modules that are available on the system $dscResourceCache = Invoke-DscCacheRefresh -module $dscResourceModules - if ($dscResourceCache.count -lt $dscResourceModules.count) { - $trace = @{'Debug' = 'ERROR: DSC resource module not found.' } | ConvertTo-Json -Compress - $host.ui.WriteErrorLine($trace) - exit 1 + + # check if all the desired modules are in the cache + $moduleInput | ForEach-Object { + if ($dscResourceCache.type -notcontains $_) { + ('DSC resource {0} module not found.' -f $_) | Write-DscTrace -Operation Error + exit 1 + } } foreach ($ds in $desiredState) {