diff --git a/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psd1 b/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psd1 index 62e3fdeb..8f52bda1 100644 --- a/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psd1 +++ b/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psd1 @@ -34,7 +34,7 @@ VariablesToExport = @() AliasesToExport = @() # DSC resources to export from this module -DscResourcesToExport = 'TestClassResource' +DscResourcesToExport = @('TestClassResource', 'NoExport') # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. PrivateData = @{ diff --git a/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 b/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 index 71eb6afc..bfe3f8f9 100644 --- a/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 +++ b/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 @@ -74,6 +74,33 @@ class TestClassResource : BaseTestClass } } +[DscResource()] +class NoExport: BaseTestClass +{ + [DscProperty(Key)] + [string] $Name + + [DscProperty()] + [string] $Prop1 + + [DscProperty()] + [string] $EnumProp + + [void] Set() + { + } + + [bool] Test() + { + return $true + } + + [NoExport] Get() + { + return $this + } +} + function Test-World() { "Hello world from PSTestModule!" diff --git a/powershell-adapter/Tests/powershellgroup.config.tests.ps1 b/powershell-adapter/Tests/powershellgroup.config.tests.ps1 index cd152d69..70f31bd7 100644 --- a/powershell-adapter/Tests/powershellgroup.config.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.config.tests.ps1 @@ -71,6 +71,23 @@ Describe 'PowerShell adapter resource tests' { $res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1" } + It 'Export fails when class-based resource does not implement' { + $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: TestClassResource/NoExport +'@ + $out = $yaml | dsc config export 2>&1 | Out-String + $LASTEXITCODE | Should -Be 2 + $out | Should -Not -BeNullOrEmpty + $out | Should -BeLike "*ERROR*Export method not implemented by resource 'TestClassResource/NoExport'*" + } + It 'Custom psmodulepath in config works' { $OldPSModulePath = $env:PSModulePath diff --git a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 index 79174668..debb9768 100644 --- a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 @@ -475,6 +475,10 @@ function Invoke-DscOperation { 'Export' { $t = $dscResourceInstance.GetType() $method = $t.GetMethod('Export') + if ($null -eq $method) { + "Export method not implemented by resource '$($DesiredState.Type)'" | Write-DscTrace -Operation Error + exit 1 + } $resultArray = $method.Invoke($null,$null) $addToActualState = $resultArray }