Skip to content

Commit a4019ca

Browse files
committed
Allow workaround for class-based WinPS
1 parent 26d226a commit a4019ca

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

powershell-adapter/Tests/win_powershellgroup.tests.ps1

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,69 @@ 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 DSC resources' -Skip:(!$IsWindows) {
273+
BeforeDiscovery {
274+
$windowsPowerShellPath = Join-Path $env:SystemRoot 'System32' 'WindowsPowerShell' 'v1.0' 'Modules'
275+
276+
$moduleFile = @"
277+
@{
278+
RootModule = 'PSClassResource.psm1'
279+
ModuleVersion = '0.1.0'
280+
GUID = '1b2e177b-1819-4f51-8bc9-795dd8fae984'
281+
Author = 'Microsoft Corporation'
282+
CompanyName = 'Microsoft Corporation'
283+
Copyright = '(c) Microsoft Corporation. All rights reserved.'
284+
Description = 'DSC Resource for Windows PowerShell Class'
285+
PowerShellVersion = '5.1'
286+
DscResourcesToExport = @(
287+
'PSClassResource'
288+
)
289+
PrivateData = @{
290+
PSData = @{
291+
Tags = @(
292+
'PSDscResource_PSClassResource'
293+
)
294+
}
295+
}
296+
}
297+
"@
298+
$moduleFilePath = Join-Path $windowsPowerShellPath 'PSClassResource' '0.1.0' 'PSClassResource.psd1'
299+
if (-not (Test-Path -Path $moduleFilePath)) {
300+
New-Item -Path $moduleFilePath -ItemType File -Value $moduleFile -Force | Out-Null
301+
}
302+
303+
304+
$module = @'
305+
[DSCResource()]
306+
class PSClassResource {
307+
[DscProperty(Key)]
308+
[string] $Name
309+
310+
PSClassResource() {
311+
}
312+
313+
[PSClassResource] Get() {
314+
return $this
315+
}
316+
317+
[bool] Test() {
318+
return $true
319+
}
320+
321+
[void] Set() {
322+
323+
}
271324
}
325+
'@
326+
327+
$modulePath = Join-Path $windowsPowerShellPath 'PSClassResource' '0.1.0' 'PSClassResource.psm1'
328+
if (-not (Test-Path -Path $modulePath)) {
329+
New-Item -Path $modulePath -ItemType File -Value $module -Force | Out-Null
330+
}
331+
}
332+
333+
$resources = dsc -l trace resource list --adapter Microsoft.Windows/WindowsPowerShell | ConvertFrom-Json
334+
$resources.type | Should -Contain 'PSClassResource/PSClassResource'
335+
}
336+
}

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 SilentlyContinue
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)