diff --git a/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 b/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 index bfe3f8f9a..641cee6bb 100644 --- a/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 +++ b/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 @@ -26,6 +26,15 @@ class TestClassResource : BaseTestClass [DscProperty()] [string] $EnumProp + [string] $NonDscProperty # This property shouldn't be in results data + + hidden + [string] $HiddenNonDscProperty # This property shouldn't be in results data + + hidden + [DscProperty()] + [string] $HiddenDscProperty # This property should be in results data, but is an anti-pattern. + [void] Set() { } diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index a9052ef7b..a15e6b4c0 100644 --- a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 @@ -37,6 +37,11 @@ Describe 'PowerShell adapter resource tests' { $LASTEXITCODE | Should -Be 0 $res = $r | ConvertFrom-Json $res.actualState.result.properties.Prop1 | Should -BeExactly 'ValueForProp1' + + # verify that only properties with DscProperty attribute are returned + $propertiesNames = $res.actualState.result.properties | Get-Member -MemberType NoteProperty | % Name + $propertiesNames | Should -Not -Contain 'NonDscProperty' + $propertiesNames | Should -Not -Contain 'HiddenNonDscProperty' } It 'Get uses enum names on class-based resource' { @@ -53,6 +58,11 @@ Describe 'PowerShell adapter resource tests' { $LASTEXITCODE | Should -Be 0 $res = $r | ConvertFrom-Json $res.actualState.result.properties.InDesiredState | Should -Be $True + + # verify that only properties with DscProperty attribute are returned + $propertiesNames = $res.actualState.result.properties.InDesiredState | Get-Member -MemberType NoteProperty | % Name + $propertiesNames | Should -Not -Contain 'NonDscProperty' + $propertiesNames | Should -Not -Contain 'HiddenNonDscProperty' } It 'Set works on class-based resource' { @@ -71,6 +81,13 @@ Describe 'PowerShell adapter resource tests' { $res.resources[0].properties.result.count | Should -Be 5 $res.resources[0].properties.result[0].Name | Should -Be "Object1" $res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1" + + # verify that only properties with DscProperty attribute are returned + $res.resources[0].properties.result | %{ + $propertiesNames = $_ | Get-Member -MemberType NoteProperty | % Name + $propertiesNames | Should -Not -Contain 'NonDscProperty' + $propertiesNames | Should -Not -Contain 'HiddenNonDscProperty' + } } It 'Get --all works on PS class-based resource' { diff --git a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 index 610503876..e3d6272ca 100644 --- a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 @@ -460,6 +460,8 @@ function Invoke-DscOperation { $resource = GetTypeInstanceFromModule -modulename $cachedDscResourceInfo.ModuleName -classname $cachedDscResourceInfo.Name $dscResourceInstance = $resource::New() + $ValidProperties = $cachedDscResourceInfo.Properties.Name + if ($DesiredState.properties) { # set each property of $dscResourceInstance to the value of the property in the $desiredState INPUT object $DesiredState.properties.psobject.properties | ForEach-Object -Process { @@ -469,14 +471,18 @@ function Invoke-DscOperation { switch ($Operation) { 'Get' { - $Result = $dscResourceInstance.Get() + $Result = @{} + $raw_obj = $dscResourceInstance.Get() + $ValidProperties | ForEach-Object { $Result[$_] = $raw_obj.$_ } $addToActualState.properties = $Result } 'Set' { $dscResourceInstance.Set() } 'Test' { - $Result = $dscResourceInstance.Test() + $Result = @{} + $raw_obj = $dscResourceInstance.Test() + $ValidProperties | ForEach-Object { $Result[$_] = $raw_obj.$_ } $addToActualState.properties = [psobject]@{'InDesiredState'=$Result} } 'Export' { @@ -486,7 +492,13 @@ function Invoke-DscOperation { "Export method not implemented by resource '$($DesiredState.Type)'" | Write-DscTrace -Operation Error exit 1 } - $resultArray = $method.Invoke($null,$null) + $resultArray = @() + $raw_obj_array = $method.Invoke($null,$null) + foreach ($raw_obj in $raw_obj_array) { + $Result_obj = @{} + $ValidProperties | ForEach-Object { $Result_obj[$_] = $raw_obj.$_ } + $resultArray += $Result_obj + } $addToActualState = $resultArray } }