From a057a3135ea2b132440542d34325e70d11288c0b Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 19 Sep 2024 13:54:31 -0700 Subject: [PATCH 1/5] updated psDscAdapter.psm1 --- .../psDscAdapter/psDscAdapter.psm1 | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 index 610503876..1d2febce4 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 | %{ $Result[$_] = $raw_obj[$_] } $addToActualState.properties = $Result } 'Set' { $dscResourceInstance.Set() } 'Test' { - $Result = $dscResourceInstance.Test() + $Result = @{} + $raw_obj = $dscResourceInstance.Test() + $ValidProperties | %{ $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 | %{ $Result_obj[$_] = $raw_obj[$_] } + $resultArray += $Result_obj + } $addToActualState = $resultArray } } From 68ff646851c55ef3759c1998480500e8a84c1f14 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 19 Sep 2024 13:58:10 -0700 Subject: [PATCH 2/5] updated TestClassResource.psm1 --- .../Tests/TestClassResource/0.0.1/TestClassResource.psm1 | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 b/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 index bfe3f8f9a..000f2d8ee 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 + [void] Set() { } From 238bd9ac8f0d331d59b8810f8f09254cd250ac51 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 19 Sep 2024 14:14:18 -0700 Subject: [PATCH 3/5] update 3 --- powershell-adapter/psDscAdapter/psDscAdapter.psm1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 index 1d2febce4..cb9387a09 100644 --- a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 @@ -473,7 +473,7 @@ function Invoke-DscOperation { 'Get' { $Result = @{} $raw_obj = $dscResourceInstance.Get() - $ValidProperties | %{ $Result[$_] = $raw_obj[$_] } + $ValidProperties | %{ $Result[$_] = $raw_obj.$_ } $addToActualState.properties = $Result } 'Set' { @@ -482,7 +482,7 @@ function Invoke-DscOperation { 'Test' { $Result = @{} $raw_obj = $dscResourceInstance.Test() - $ValidProperties | %{ $Result[$_] = $raw_obj[$_] } + $ValidProperties | %{ $Result[$_] = $raw_obj.$_ } $addToActualState.properties = [psobject]@{'InDesiredState'=$Result} } 'Export' { @@ -496,7 +496,7 @@ function Invoke-DscOperation { $raw_obj_array = $method.Invoke($null,$null) foreach ($raw_obj in $raw_obj_array) { $Result_obj = @{} - $ValidProperties | %{ $Result_obj[$_] = $raw_obj[$_] } + $ValidProperties | %{ $Result_obj[$_] = $raw_obj.$_ } $resultArray += $Result_obj } $addToActualState = $resultArray From eb736290128f28d47f37ef8700670b196a0421bc Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 19 Sep 2024 14:39:55 -0700 Subject: [PATCH 4/5] update 4 --- .../0.0.1/TestClassResource.psm1 | 2 +- .../Tests/powershellgroup.resource.tests.ps1 | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 b/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 index 000f2d8ee..641cee6bb 100644 --- a/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 +++ b/powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1 @@ -33,7 +33,7 @@ class TestClassResource : BaseTestClass hidden [DscProperty()] - [string] $HiddenDscProperty + [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' { From 5af38f6010baa04018978907ecc63ba90994a884 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 19 Sep 2024 19:43:43 -0700 Subject: [PATCH 5/5] update 5 --- powershell-adapter/psDscAdapter/psDscAdapter.psm1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 index cb9387a09..e3d6272ca 100644 --- a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 @@ -473,7 +473,7 @@ function Invoke-DscOperation { 'Get' { $Result = @{} $raw_obj = $dscResourceInstance.Get() - $ValidProperties | %{ $Result[$_] = $raw_obj.$_ } + $ValidProperties | ForEach-Object { $Result[$_] = $raw_obj.$_ } $addToActualState.properties = $Result } 'Set' { @@ -482,7 +482,7 @@ function Invoke-DscOperation { 'Test' { $Result = @{} $raw_obj = $dscResourceInstance.Test() - $ValidProperties | %{ $Result[$_] = $raw_obj.$_ } + $ValidProperties | ForEach-Object { $Result[$_] = $raw_obj.$_ } $addToActualState.properties = [psobject]@{'InDesiredState'=$Result} } 'Export' { @@ -496,7 +496,7 @@ function Invoke-DscOperation { $raw_obj_array = $method.Invoke($null,$null) foreach ($raw_obj in $raw_obj_array) { $Result_obj = @{} - $ValidProperties | %{ $Result_obj[$_] = $raw_obj.$_ } + $ValidProperties | ForEach-Object { $Result_obj[$_] = $raw_obj.$_ } $resultArray += $Result_obj } $addToActualState = $resultArray