Skip to content

Fix WinPS adapter hidden properties #855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
4 changes: 4 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ if ($null -ne $packageType) {
Remove-Item temp:/rustup-init.exe -ErrorAction Ignore
}
}
else {
Write-Verbose -Verbose "Rust found, updating..."
& $rustup update
}

$BuildToolsPath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC"

Expand Down
26 changes: 25 additions & 1 deletion powershell-adapter/Tests/win_powershellgroup.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ resources:
Tags = @(
'PSDscResource_PSClassResource'
)
DscCapabilities = @(
'get'
'test'
'set'
'export'
)
}
}
}
Expand All @@ -303,11 +309,24 @@ resources:


$module = @'
enum Ensure {
Present
Absent
}

[DSCResource()]
class PSClassResource {
[DscProperty(Key)]
[string] $Name

[string] $NonDscProperty

hidden
[string] $HiddenNonDscProperty

[DscProperty()]
[Ensure] $Ensure = [Ensure]::Present

PSClassResource() {
}

Expand All @@ -333,6 +352,7 @@ class PSClassResource {
1..$resultCount | %{
$obj = New-Object PSClassResource
$obj.Name = "Object$_"
$obj.Ensure = [Ensure]::Present
$resultList.Add($obj)
}

Expand All @@ -358,6 +378,8 @@ class PSClassResource {
$out = dsc resource get -r PSClassResource/PSClassResource --input (@{Name = 'TestName' } | ConvertTo-Json) | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.actualState.Name | Should -Be 'TestName'
$propCount = $out.actualState | Get-Member -MemberType NoteProperty
$propCount.Count | Should -Be 1 # Only the DscProperty should be returned
}

It 'Set works with class-based PS DSC resources' -Skip:(!$IsWindows) {
Expand All @@ -373,5 +395,7 @@ class PSClassResource {
$LASTEXITCODE | Should -Be 0
$out | Should -Not -BeNullOrEmpty
$out.resources.count | Should -Be 5
$out.resources[0].properties.Ensure | Should -Be 'Present' # Check for enum property
}
}
}

15 changes: 14 additions & 1 deletion powershell-adapter/psDscAdapter/win_psDscAdapter.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,10 @@ function Invoke-DscOperation {
$resource = GetTypeInstanceFromModule -modulename $cachedDscResourceInfo.ModuleName -classname $cachedDscResourceInfo.Name
$dscResourceInstance = $resource::New()

$ValidProperties = $cachedDscResourceInfo.Properties.Name

$ValidProperties | ConvertTo-Json | Write-DscTrace -Operation Trace

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 {
Expand All @@ -427,7 +431,16 @@ function Invoke-DscOperation {

switch ($Operation) {
'Get' {
$Result = $dscResourceInstance.Get()
$Result = @{}
$raw_obj = $dscResourceInstance.Get()
$ValidProperties | ForEach-Object {
if ($raw_obj.$_ -is [System.Enum]) {
$Result[$_] = $raw_obj.$_.ToString()
}
else {
$Result[$_] = $raw_obj.$_
}
}
$addToActualState.properties = $Result
}
'Set' {
Expand Down
Loading