Skip to content

Commit 47c0d4f

Browse files
authored
Merge pull request #797 from Gijsreyn/enable-class-based-winps
Allow workaround for class-based WinPS
2 parents ea2d0f3 + 891d2ec commit 47c0d4f

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

powershell-adapter/Tests/win_powershellgroup.tests.ps1

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,86 @@ resources:
268268
$out | Should -Not -BeNullOrEmpty
269269
$out | Should -BeLike "*ERROR*Credential object 'Credential' requires both 'username' and 'password' properties*"
270270
}
271+
272+
It 'List works with class-based PS DSC resources' -Skip:(!$IsWindows) {
273+
BeforeDiscovery {
274+
$windowsPowerShellPath = Join-Path $testDrive 'WindowsPowerShell' 'Modules'
275+
$env:PSModulePath += [System.IO.Path]::PathSeparator + $windowsPowerShellPath
276+
277+
$moduleFile = @"
278+
@{
279+
RootModule = 'PSClassResource.psm1'
280+
ModuleVersion = '0.1.0'
281+
GUID = '1b2e177b-1819-4f51-8bc9-795dd8fae984'
282+
Author = 'Microsoft Corporation'
283+
CompanyName = 'Microsoft Corporation'
284+
Copyright = '(c) Microsoft Corporation. All rights reserved.'
285+
Description = 'DSC Resource for Windows PowerShell Class'
286+
PowerShellVersion = '5.1'
287+
DscResourcesToExport = @(
288+
'PSClassResource'
289+
)
290+
PrivateData = @{
291+
PSData = @{
292+
Tags = @(
293+
'PSDscResource_PSClassResource'
294+
)
295+
}
296+
}
271297
}
298+
"@
299+
$moduleFilePath = Join-Path $windowsPowerShellPath 'PSClassResource' '0.1.0' 'PSClassResource.psd1'
300+
if (-not (Test-Path -Path $moduleFilePath)) {
301+
New-Item -Path $moduleFilePath -ItemType File -Value $moduleFile -Force | Out-Null
302+
}
303+
304+
305+
$module = @'
306+
[DSCResource()]
307+
class PSClassResource {
308+
[DscProperty(Key)]
309+
[string] $Name
310+
311+
PSClassResource() {
312+
}
313+
314+
[PSClassResource] Get() {
315+
return $this
316+
}
317+
318+
[bool] Test() {
319+
return $true
320+
}
321+
322+
[void] Set() {
323+
324+
}
325+
}
326+
'@
327+
328+
$modulePath = Join-Path $windowsPowerShellPath 'PSClassResource' '0.1.0' 'PSClassResource.psm1'
329+
if (-not (Test-Path -Path $modulePath)) {
330+
New-Item -Path $modulePath -ItemType File -Value $module -Force | Out-Null
331+
}
332+
}
333+
334+
$out = dsc -l trace resource list --adapter Microsoft.Windows/WindowsPowerShell | ConvertFrom-Json
335+
$LASTEXITCODE | Should -Be 0
336+
$out.type | Should -Contain 'PSClassResource/PSClassResource'
337+
$out | Where-Object -Property type -EQ PSClassResource/PSClassResource | Select-Object -ExpandProperty implementedAs | Should -Be 1 # Class-based
338+
}
339+
340+
It 'Get works with class-based PS DSC resources' -Skip:(!$IsWindows) {
341+
342+
$out = dsc resource get -r PSClassResource/PSClassResource --input (@{Name = 'TestName' } | ConvertTo-Json) | ConvertFrom-Json
343+
$LASTEXITCODE | Should -Be 0
344+
$out.actualState.Name | Should -Be 'TestName'
345+
}
346+
347+
It 'Set works with class-based PS DSC resources' -Skip:(!$IsWindows) {
348+
349+
$out = dsc resource set -r PSClassResource/PSClassResource --input (@{Name = 'TestName' } | ConvertTo-Json) | ConvertFrom-Json
350+
$LASTEXITCODE | Should -Be 0
351+
$out.afterstate.InDesiredState | Should -Be $true
352+
}
353+
}

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ function Invoke-DscCacheRefresh {
223223
}
224224
}
225225

226+
# workaround: Use GetTypeInstanceFromModule to get the type instance from the module and validate if it is a class-based resource
227+
$classBased = GetTypeInstanceFromModule -modulename $moduleName -classname $dscResource.Name -ErrorAction Ignore
228+
if (-not ([string]::IsNullOrEmpty($classBased))) {
229+
$dscResourceInfo.ImplementationDetail = 'ClassBased'
230+
}
231+
226232
# fill in resource files (and their last-write-times) that will be used for up-do-date checks
227233
$lastWriteTimes = @{}
228234
Get-ChildItem -Recurse -File -Path $dscResource.ParentPath -Include "*.ps1", "*.psd1", "*.psm1", "*.mof" -ea Ignore | % {

0 commit comments

Comments
 (0)