Skip to content

Commit f7ef3cb

Browse files
authoredMay 22, 2024··
Merge pull request #435 from anmenaga/winps
Separated WinPS adapter and tests
2 parents 06e79cd + f8f4fda commit f7ef3cb

9 files changed

+645
-254
lines changed
 

‎powershell-adapter/Tests/class_ps_resources.dsc.yaml

-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ resources:
77
type: Microsoft.DSC/PowerShell
88
properties:
99
resources:
10-
- name: Repository Info
11-
type: PSTestModule/TestPSRepository
12-
properties:
13-
Name: TestPSRepository1
1410
- name: Class-resource Info
1511
type: TestClassResource/TestClassResource
1612
properties:

‎powershell-adapter/Tests/powershellgroup.config.tests.ps1

+6-26
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,16 @@
44
Describe 'PowerShell adapter resource tests' {
55

66
BeforeAll {
7-
if ($isWindows) {
8-
winrm quickconfig -quiet -force
9-
}
107
$OldPSModulePath = $env:PSModulePath
118
$env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot
129
$pwshConfigPath = Join-path $PSScriptRoot "class_ps_resources.dsc.yaml"
13-
$winpsConfigPath = Join-path $PSScriptRoot "winps_resource.dsc.yaml"
1410

1511
if ($IsLinux -or $IsMacOS) {
1612
$cacheFilePath = Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
1713
}
1814
else
1915
{
2016
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json"
21-
$cacheFilePath_v5 = Join-Path $env:LocalAppData "dsc" "WindowsPSAdapterCache.json"
2217
}
2318
}
2419
AfterAll {
@@ -27,47 +22,32 @@ Describe 'PowerShell adapter resource tests' {
2722

2823
BeforeEach {
2924
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath
30-
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath_v5
3125
}
3226

33-
It 'Get works on config with class-based and script-based resources' -Skip:(!$IsWindows){
27+
It 'Get works on config with class-based resources' -Skip:(!$IsWindows){
3428

3529
$r = Get-Content -Raw $pwshConfigPath | dsc config get
3630
$LASTEXITCODE | Should -Be 0
3731
$res = $r | ConvertFrom-Json
38-
$res.results[0].result.actualState.result[0].properties.PublishLocation | Should -BeExactly 'https://www.powershellgallery.com/api/v2/package/'
39-
$res.results[0].result.actualState.result[1].properties.Prop1 | Should -BeExactly 'ValueForProp1'
40-
$res.results[0].result.actualState.result[1].properties.EnumProp | Should -BeExactly 'Expected'
41-
}
42-
43-
It 'Get works on config with File resource for WinPS' -Skip:(!$IsWindows){
44-
45-
$testFile = "$testdrive\test.txt"
46-
'test' | Set-Content -Path $testFile -Force
47-
$r = (Get-Content -Raw $winpsConfigPath).Replace('c:\test.txt',"$testFile") | dsc config get
48-
$LASTEXITCODE | Should -Be 0
49-
$res = $r | ConvertFrom-Json
50-
$res.results[0].result.actualState.result[0].properties.DestinationPath | Should -Be "$testFile"
32+
$res.results[0].result.actualState.result[0].properties.Prop1 | Should -BeExactly 'ValueForProp1'
33+
$res.results[0].result.actualState.result[0].properties.EnumProp | Should -BeExactly 'Expected'
5134
}
5235

53-
It 'Test works on config with class-based and script-based resources' -Skip:(!$IsWindows){
36+
It 'Test works on config with class-based resources' -Skip:(!$IsWindows){
5437

5538
$r = Get-Content -Raw $pwshConfigPath | dsc config test
5639
$LASTEXITCODE | Should -Be 0
5740
$res = $r | ConvertFrom-Json
5841
$res.results[0].result.actualState.result[0] | Should -Not -BeNull
59-
$res.results[0].result.actualState.result[1] | Should -Not -BeNull
6042
}
6143

62-
It 'Set works on config with class-based and script-based resources' -Skip:(!$IsWindows){
44+
It 'Set works on config with class-based resources' -Skip:(!$IsWindows){
6345

6446
$r = Get-Content -Raw $pwshConfigPath | dsc config set
6547
$LASTEXITCODE | Should -Be 0
6648
$res = $r | ConvertFrom-Json
67-
$res.results.result.afterState.result[0].type | Should -Be "PSTestModule/TestPSRepository"
68-
$res.results.result.afterState.result[1].type | Should -Be "TestClassResource/TestClassResource"
49+
$res.results.result.afterState.result[0].type | Should -Be "TestClassResource/TestClassResource"
6950
}
70-
7151

7252
It 'Export works on config with class-based resources' -Skip:(!$IsWindows){
7353

‎powershell-adapter/Tests/powershellgroup.resource.tests.ps1

-65
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
Describe 'PowerShell adapter resource tests' {
55

66
BeforeAll {
7-
if ($isWindows) {
8-
winrm quickconfig -quiet -force
9-
}
107
$OldPSModulePath = $env:PSModulePath
118
$env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot
129

@@ -16,7 +13,6 @@ Describe 'PowerShell adapter resource tests' {
1613
else
1714
{
1815
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json"
19-
$cacheFilePath_v5 = Join-Path $env:LocalAppData "dsc" "WindowsPSAdapterCache.json"
2016
}
2117
}
2218
AfterAll {
@@ -25,7 +21,6 @@ Describe 'PowerShell adapter resource tests' {
2521

2622
BeforeEach {
2723
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath
28-
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath_v5
2924
}
3025

3126
It 'Discovery includes class-based and script-based resources ' -Skip:(!$IsWindows){
@@ -37,34 +32,6 @@ Describe 'PowerShell adapter resource tests' {
3732
($resources | ? {$_.Type -eq 'PSTestModule/TestPSRepository'}).Count | Should -Be 1
3833
}
3934

40-
It 'Windows PowerShell adapter supports File resource' -Skip:(!$IsWindows){
41-
42-
$r = dsc resource list --adapter Microsoft.Windows/WindowsPowerShell
43-
$LASTEXITCODE | Should -Be 0
44-
$resources = $r | ConvertFrom-Json
45-
($resources | ? {$_.Type -eq 'PSDesiredStateConfiguration/File'}).Count | Should -Be 1
46-
}
47-
48-
It 'Get works on Binary "File" resource' -Skip:(!$IsWindows){
49-
50-
$testFile = "$testdrive\test.txt"
51-
'test' | Set-Content -Path $testFile -Force
52-
$r = '{"DestinationPath":"' + $testFile.replace('\','\\') + '"}' | dsc resource get -r 'PSDesiredStateConfiguration/File'
53-
$LASTEXITCODE | Should -Be 0
54-
$res = $r | ConvertFrom-Json
55-
$res.actualState.result.properties.DestinationPath | Should -Be "$testFile"
56-
}
57-
58-
It 'Get works on traditional "Script" resource' -Skip:(!$IsWindows){
59-
60-
$testFile = "$testdrive\test.txt"
61-
'test' | Set-Content -Path $testFile -Force
62-
$r = '{"GetScript": "@{result = $(Get-Content ' + $testFile.replace('\','\\') + ')}", "SetScript": "throw", "TestScript": "throw"}' | dsc resource get -r 'PSDesiredStateConfiguration/Script'
63-
$LASTEXITCODE | Should -Be 0
64-
$res = $r | ConvertFrom-Json
65-
$res.actualState.result.properties.result | Should -Be 'test'
66-
}
67-
6835
It 'Get works on class-based resource' -Skip:(!$IsWindows){
6936

7037
$r = "{'Name':'TestClassResource1'}" | dsc resource get -r 'TestClassResource/TestClassResource'
@@ -73,14 +40,6 @@ Describe 'PowerShell adapter resource tests' {
7340
$res.actualState.result.properties.Prop1 | Should -BeExactly 'ValueForProp1'
7441
}
7542

76-
It 'Get works on script-based resource' -Skip:(!$IsWindows){
77-
78-
$r = "{'Name':'TestPSRepository1'}" | dsc resource get -r 'PSTestModule/TestPSRepository'
79-
$LASTEXITCODE | Should -Be 0
80-
$res = $r | ConvertFrom-Json
81-
$res.actualState.result.properties.PublishLocation | Should -BeExactly 'https://www.powershellgallery.com/api/v2/package/'
82-
}
83-
8443
It 'Get uses enum names on class-based resource' -Skip:(!$IsWindows){
8544

8645
$r = "{'Name':'TestClassResource1'}" | dsc resource get -r 'TestClassResource/TestClassResource'
@@ -89,14 +48,6 @@ Describe 'PowerShell adapter resource tests' {
8948
$res.actualState.result.properties.EnumProp | Should -BeExactly 'Expected'
9049
}
9150

92-
It 'Get uses enum names on script-based resource' -Skip:(!$IsWindows){
93-
94-
$r = "{'Name':'TestPSRepository1'}" | dsc resource get -r 'PSTestModule/TestPSRepository'
95-
$LASTEXITCODE | Should -Be 0
96-
$res = $r | ConvertFrom-Json
97-
$res.actualState.result.properties.Ensure | Should -BeExactly 'Present'
98-
}
99-
10051
It 'Test works on class-based resource' -Skip:(!$IsWindows){
10152

10253
$r = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc resource test -r 'TestClassResource/TestClassResource'
@@ -105,14 +56,6 @@ Describe 'PowerShell adapter resource tests' {
10556
$res.actualState.result.properties.InDesiredState | Should -Be $True
10657
}
10758

108-
It 'Test works on script-based resource' -Skip:(!$IsWindows){
109-
110-
$r = "{'Name':'TestPSRepository1','PackageManagementProvider':'NuGet'}" | dsc resource test -r 'PSTestModule/TestPSRepository'
111-
$LASTEXITCODE | Should -Be 0
112-
$res = $r | ConvertFrom-Json
113-
$res.actualState.result.properties.InDesiredState | Should -Be $True
114-
}
115-
11659
It 'Set works on class-based resource' -Skip:(!$IsWindows){
11760

11861
$r = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc resource set -r 'TestClassResource/TestClassResource'
@@ -121,14 +64,6 @@ Describe 'PowerShell adapter resource tests' {
12164
$res.afterState.result | Should -Not -BeNull
12265
}
12366

124-
It 'Set works on script-based resource' -Skip:(!$IsWindows){
125-
126-
$r = "{'Name':'TestPSRepository1'}" | dsc resource set -r 'PSTestModule/TestPSRepository'
127-
$LASTEXITCODE | Should -Be 0
128-
$res = $r | ConvertFrom-Json
129-
$res.afterState.result.properties.RebootRequired | Should -Not -BeNull
130-
}
131-
13267
It 'Export works on PS class-based resource' -Skip:(!$IsWindows){
13368

13469
$r = dsc resource export -r TestClassResource/TestClassResource
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
Describe 'WindowsPowerShell adapter resource tests' {
5+
6+
BeforeAll {
7+
if ($isWindows) {
8+
winrm quickconfig -quiet -force
9+
}
10+
$OldPSModulePath = $env:PSModulePath
11+
$env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot
12+
13+
$winpsConfigPath = Join-path $PSScriptRoot "winps_resource.dsc.yaml"
14+
if ($isWindows) {
15+
$cacheFilePath_v5 = Join-Path $env:LocalAppData "dsc" "WindowsPSAdapterCache.json"
16+
}
17+
}
18+
AfterAll {
19+
$env:PSModulePath = $OldPSModulePath
20+
}
21+
22+
BeforeEach {
23+
if ($isWindows) {
24+
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath_v5
25+
}
26+
}
27+
28+
It 'Windows PowerShell adapter supports File resource' -Skip:(!$IsWindows){
29+
30+
$r = dsc resource list --adapter Microsoft.Windows/WindowsPowerShell
31+
$LASTEXITCODE | Should -Be 0
32+
$resources = $r | ConvertFrom-Json
33+
($resources | ? {$_.Type -eq 'PSDesiredStateConfiguration/File'}).Count | Should -Be 1
34+
}
35+
36+
It 'Get works on Binary "File" resource' -Skip:(!$IsWindows){
37+
38+
$testFile = "$testdrive\test.txt"
39+
'test' | Set-Content -Path $testFile -Force
40+
$r = '{"DestinationPath":"' + $testFile.replace('\','\\') + '"}' | dsc resource get -r 'PSDesiredStateConfiguration/File'
41+
$LASTEXITCODE | Should -Be 0
42+
$res = $r | ConvertFrom-Json
43+
$res.actualState.result.properties.DestinationPath | Should -Be "$testFile"
44+
}
45+
46+
It 'Get works on traditional "Script" resource' -Skip:(!$IsWindows){
47+
48+
$testFile = "$testdrive\test.txt"
49+
'test' | Set-Content -Path $testFile -Force
50+
$r = '{"GetScript": "@{result = $(Get-Content ' + $testFile.replace('\','\\') + ')}", "SetScript": "throw", "TestScript": "throw"}' | dsc resource get -r 'PSDesiredStateConfiguration/Script'
51+
$LASTEXITCODE | Should -Be 0
52+
$res = $r | ConvertFrom-Json
53+
$res.actualState.result.properties.result | Should -Be 'test'
54+
}
55+
56+
It 'Get works on config with File resource for WinPS' -Skip:(!$IsWindows){
57+
58+
$testFile = "$testdrive\test.txt"
59+
'test' | Set-Content -Path $testFile -Force
60+
$r = (Get-Content -Raw $winpsConfigPath).Replace('c:\test.txt',"$testFile") | dsc config get
61+
$LASTEXITCODE | Should -Be 0
62+
$res = $r | ConvertFrom-Json
63+
$res.results[0].result.actualState.result[0].properties.DestinationPath | Should -Be "$testFile"
64+
}
65+
}

‎powershell-adapter/copy_files.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
./psDscAdapter/powershell.resource.ps1
22
./psDscAdapter/psDscAdapter.psd1
3-
./psDscAdapter/psDscAdapter.psm1
3+
./psDscAdapter/psDscAdapter.psm1
4+
./psDscAdapter/win_psDscAdapter.psd1
5+
./psDscAdapter/win_psDscAdapter.psm1

‎powershell-adapter/psDscAdapter/powershell.resource.ps1

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ if ('Validate' -ne $Operation) {
1515
$host.ui.WriteErrorLine($trace)
1616

1717
# load private functions of psDscAdapter stub module
18-
$psDscAdapter = Import-Module "$PSScriptRoot/psDscAdapter.psd1" -Force -PassThru
18+
if ($PSVersionTable.PSVersion.Major -le 5) {
19+
$psDscAdapter = Import-Module "$PSScriptRoot/win_psDscAdapter.psd1" -Force -PassThru
20+
}
21+
else {
22+
$psDscAdapter = Import-Module "$PSScriptRoot/psDscAdapter.psd1" -Force -PassThru
23+
}
24+
1925

2026
# initialize OUTPUT as array
2127
$result = [System.Collections.Generic.List[Object]]::new()

‎powershell-adapter/psDscAdapter/psDscAdapter.psm1

+8-157
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,10 @@ function Write-DscTrace {
1515
$host.ui.WriteErrorLine($trace)
1616
}
1717

18-
# if the version of PowerShell is greater than 5, import the PSDesiredStateConfiguration module
19-
# this is necessary because the module is not included in the PowerShell 7.0+ releases;
20-
# In Windows PowerShell, we should always use version 1.1 that ships in Windows.
21-
if ($PSVersionTable.PSVersion.Major -gt 5) {
18+
function Import-PSDSCModule {
2219
$m = Get-Module PSDesiredStateConfiguration -ListAvailable | Sort-Object -Descending | Select-Object -First 1
2320
$PSDesiredStateConfiguration = Import-Module $m -Force -PassThru
2421
}
25-
else {
26-
$env:PSModulePath += ";$env:windir\System32\WindowsPowerShell\v1.0\Modules"
27-
$PSDesiredStateConfiguration = Import-Module -Name 'PSDesiredStateConfiguration' -RequiredVersion '1.1' -Force -PassThru -ErrorAction stop -ErrorVariable $importModuleError
28-
if (-not [string]::IsNullOrEmpty($importModuleError)) {
29-
'ERROR: Could not import PSDesiredStateConfiguration 1.1 in Windows PowerShell. ' + $importModuleError | Write-DscTrace
30-
}
31-
}
3222

3323
<# public function Invoke-DscCacheRefresh
3424
.SYNOPSIS
@@ -123,54 +113,23 @@ function Invoke-DscCacheRefresh {
123113
# create a list object to store cache of Get-DscResource
124114
[dscResourceCacheEntry[]]$dscResourceCacheEntries = [System.Collections.Generic.List[Object]]::new()
125115

126-
# improve by performance by having the option to only get details for named modules
127-
# workaround for File and SignatureValidation resources that ship in Windows
128-
if ($null -ne $module -and 'PSDesiredStateConfiguration' -ne $module) {
129-
if ($module.gettype().name -eq 'string') {
130-
$module = @($module)
131-
}
132-
$DscResources = [System.Collections.Generic.List[Object]]::new()
133-
$Modules = [System.Collections.Generic.List[Object]]::new()
134-
foreach ($m in $module) {
135-
$DscResources += Get-DscResource -Module $m
136-
$Modules += Get-Module -Name $m -ListAvailable
137-
}
138-
}
139-
elseif ('PSDesiredStateConfiguration' -eq $module -and $PSVersionTable.PSVersion.Major -le 5 ) {
140-
# the resources in Windows should only load in Windows PowerShell
141-
# workaround: the binary modules don't have a module name, so we have to special case File and SignatureValidation resources that ship in Windows
142-
$DscResources = Get-DscResource | Where-Object { $_.modulename -eq 'PSDesiredStateConfiguration' -or ( $_.modulename -eq $null -and $_.parentpath -like "$env:windir\System32\Configuration\*" ) }
143-
}
144-
else {
145-
# if no module is specified, get all resources
146-
$DscResources = Get-DscResource
147-
$Modules = Get-Module -ListAvailable
148-
}
149-
150-
$psdscVersion = Get-Module PSDesiredStateConfiguration | Sort-Object -descending | Select-Object -First 1 | ForEach-Object Version
116+
Import-PSDSCModule
117+
$DscResources = Get-DscResource
151118

152119
foreach ($dscResource in $DscResources) {
153120
# resources that shipped in Windows should only be used with Windows PowerShell
154121
if ($dscResource.ParentPath -like "$env:windir\System32\*" -and $PSVersionTable.PSVersion.Major -gt 5) {
155122
continue
156123
}
157124

158-
# we can't run this check in PSDesiredStateConfiguration 1.1 because the property doesn't exist
159-
if ( $psdscVersion -ge '2.0.7' ) {
125+
if ( $dscResource.ImplementationDetail ) {
160126
# only support known dscResourceType
161127
if ([dscResourceType].GetEnumNames() -notcontains $dscResource.ImplementationDetail) {
162128
'WARNING: implementation detail not found: ' + $dscResource.ImplementationDetail | Write-DscTrace
163129
continue
164130
}
165131
}
166132

167-
# workaround: if the resource does not have a module name, get it from parent path
168-
# workaround: modulename is not settable, so clone the object without being read-only
169-
# workaround: we have to special case File and SignatureValidation resources that ship in Windows
170-
$binaryBuiltInModulePaths = @(
171-
"$env:windir\system32\Configuration\Schema\MSFT_FileDirectoryConfiguration"
172-
"$env:windir\system32\Configuration\BaseRegistration"
173-
)
174133
$DscResourceInfo = [DscResourceInfo]::new()
175134
$dscResource.PSObject.Properties | ForEach-Object -Process {
176135
if ($null -ne $_.Value) {
@@ -181,30 +140,7 @@ function Invoke-DscCacheRefresh {
181140
}
182141
}
183142

184-
if ($dscResource.ModuleName) {
185-
$moduleName = $dscResource.ModuleName
186-
}
187-
elseif ($binaryBuiltInModulePaths -contains $dscResource.ParentPath) {
188-
$moduleName = 'PSDesiredStateConfiguration'
189-
$DscResourceInfo.Module = 'PSDesiredStateConfiguration'
190-
$DscResourceInfo.ModuleName = 'PSDesiredStateConfiguration'
191-
$DscResourceInfo.CompanyName = 'Microsoft Corporation'
192-
$DscResourceInfo.Version = '1.0.0'
193-
if ($PSVersionTable.PSVersion.Major -le 5 -and $DscResourceInfo.ImplementedAs -eq 'Binary') {
194-
$DscResourceInfo.ImplementationDetail = 'Binary'
195-
}
196-
}
197-
elseif ($binaryBuiltInModulePaths -notcontains $dscResource.ParentPath -and $null -ne $dscResource.ParentPath) {
198-
# workaround: populate module name from parent path that is three levels up
199-
$moduleName = Split-Path $dscResource.ParentPath | Split-Path | Split-Path -Leaf
200-
$DscResourceInfo.Module = $moduleName
201-
$DscResourceInfo.ModuleName = $moduleName
202-
# workaround: populate module version from psmoduleinfo if available
203-
if ($moduleInfo = $Modules | Where-Object { $_.Name -eq $moduleName }) {
204-
$moduleInfo = $moduleInfo | Sort-Object -Property Version -Descending | Select-Object -First 1
205-
$DscResourceInfo.Version = $moduleInfo.Version.ToString()
206-
}
207-
}
143+
$moduleName = $dscResource.ModuleName
208144

209145
# fill in resource files (and their last-write-times) that will be used for up-do-date checks
210146
$lastWriteTimes = @{}
@@ -249,13 +185,7 @@ function Get-DscResourceObject {
249185
'WARNING: The input has a top level property named "resources" but is not a configuration. If the input should be a configuration, include the property: "metadata": {"Microsoft.DSC": {"context": "Configuration"}}' | Write-DscTrace
250186
}
251187

252-
# match adapter to version of powershell
253-
if ($PSVersionTable.PSVersion.Major -le 5) {
254-
$adapterName = 'Microsoft.Windows/WindowsPowerShell'
255-
}
256-
else {
257-
$adapterName = 'Microsoft.DSC/PowerShell'
258-
}
188+
$adapterName = 'Microsoft.DSC/PowerShell'
259189

260190
if ($null -ne $inputObj.metadata -and $null -ne $inputObj.metadata.'Microsoft.DSC' -and $inputObj.metadata.'Microsoft.DSC'.context -eq 'configuration') {
261191
# change the type from pscustomobject to dscResourceObject
@@ -315,54 +245,9 @@ function Invoke-DscOperation {
315245
if ($_.TypeNameOfValue -EQ 'System.String') { $addToActualState.$($_.Name) = $DesiredState.($_.Name) }
316246
}
317247

318-
'DSC resource implementation: ' + [dscResourceType]$cachedDscResourceInfo.ImplementationDetail | Write-DscTrace
319-
320248
# workaround: script based resources do not validate Get parameter consistency, so we need to remove any parameters the author chose not to include in Get-TargetResource
321249
switch ([dscResourceType]$cachedDscResourceInfo.ImplementationDetail) {
322-
'ScriptBased' {
323-
324-
# For Linux/MacOS, only class based resources are supported and are called directly.
325-
if ($IsLinux) {
326-
'ERROR: Script based resources are only supported on Windows.' | Write-DscTrace
327-
exit 1
328-
}
329-
330-
# imports the .psm1 file for the DSC resource as a PowerShell module and stores the list of parameters
331-
Import-Module -Scope Local -Name $cachedDscResourceInfo.path -Force -ErrorAction stop
332-
$validParams = (Get-Command -Module $cachedDscResourceInfo.ResourceType -Name 'Get-TargetResource').Parameters.Keys
333-
334-
if ($Operation -eq 'Get') {
335-
# prune any properties that are not valid parameters of Get-TargetResource
336-
$DesiredState.properties.psobject.properties | ForEach-Object -Process {
337-
if ($validParams -notcontains $_.Name) {
338-
$DesiredState.properties.psobject.properties.Remove($_.Name)
339-
}
340-
}
341-
}
342-
343-
# morph the INPUT object into a hashtable named "property" for the cmdlet Invoke-DscResource
344-
$DesiredState.properties.psobject.properties | ForEach-Object -Begin { $property = @{} } -Process { $property[$_.Name] = $_.Value }
345-
346-
# using the cmdlet the appropriate dsc module, and handle errors
347-
try {
348-
$invokeResult = Invoke-DscResource -Method $Operation -ModuleName $cachedDscResourceInfo.ModuleName -Name $cachedDscResourceInfo.Name -Property $property
349-
350-
if ($invokeResult.GetType().Name -eq 'Hashtable') {
351-
$invokeResult.keys | ForEach-Object -Begin { $getDscResult = @{} } -Process { $getDscResult[$_] = $invokeResult.$_ }
352-
}
353-
else {
354-
# the object returned by WMI is a CIM instance with a lot of additional data. only return DSC properties
355-
$invokeResult.psobject.Properties.name | Where-Object { 'CimClass', 'CimInstanceProperties', 'CimSystemProperties' -notcontains $_ } | ForEach-Object -Begin { $getDscResult = @{} } -Process { $getDscResult[$_] = $invokeResult.$_ }
356-
}
357-
358-
# set the properties of the OUTPUT object from the result of Get-TargetResource
359-
$addToActualState.properties = $getDscResult
360-
}
361-
catch {
362-
'ERROR: ' + $_.Exception.Message | Write-DscTrace
363-
exit 1
364-
}
365-
}
250+
366251
'ClassBased' {
367252
try {
368253
# load powershell class from external module
@@ -402,42 +287,8 @@ function Invoke-DscOperation {
402287
exit 1
403288
}
404289
}
405-
'Binary' {
406-
if ($PSVersionTable.PSVersion.Major -gt 5) {
407-
'To use a binary resource such as File, Log, or SignatureValidation, use the Microsoft.Windows/WindowsPowerShell adapter.' | Write-DscTrace
408-
exit 1
409-
}
410-
411-
if (-not (($cachedDscResourceInfo.ImplementedAs -eq 'Binary') -and ('File', 'Log', 'SignatureValidation' -contains $cachedDscResourceInfo.Name))) {
412-
'Only File, Log, and SignatureValidation are supported as Binary resources.' | Write-DscTrace
413-
exit 1
414-
}
415-
416-
# morph the INPUT object into a hashtable named "property" for the cmdlet Invoke-DscResource
417-
$DesiredState.properties.psobject.properties | ForEach-Object -Begin { $property = @{} } -Process { $property[$_.Name] = $_.Value }
418-
419-
# using the cmdlet from PSDesiredStateConfiguration module in Windows
420-
try {
421-
$getResult = $PSDesiredStateConfiguration.invoke({ param($Name, $Property) Invoke-DscResource -Name $Name -Method Get -ModuleName @{ModuleName = 'PSDesiredStateConfiguration'; ModuleVersion = '1.1' } -Property $Property -ErrorAction Stop }, $cachedDscResourceInfo.Name, $property )
422-
423-
if ($getResult.GetType().Name -eq 'Hashtable') {
424-
$getResult.keys | ForEach-Object -Begin { $getDscResult = @{} } -Process { $getDscResult[$_] = $getResult.$_ }
425-
}
426-
else {
427-
# the object returned by WMI is a CIM instance with a lot of additional data. only return DSC properties
428-
$getResult.psobject.Properties.name | Where-Object { 'CimClass', 'CimInstanceProperties', 'CimSystemProperties' -notcontains $_ } | ForEach-Object -Begin { $getDscResult = @{} } -Process { $getDscResult[$_] = $getResult.$_ }
429-
}
430-
431-
# set the properties of the OUTPUT object from the result of Get-TargetResource
432-
$addToActualState.properties = $getDscResult
433-
}
434-
catch {
435-
'ERROR: ' + $_.Exception.Message | Write-DscTrace
436-
exit 1
437-
}
438-
}
439290
Default {
440-
'Can not find implementation of type: ' + $cachedDscResourceInfo.ImplementationDetail | Write-DscTrace
291+
'Resource ImplementationDetail not supported: ' + $cachedDscResourceInfo.ImplementationDetail | Write-DscTrace
441292
exit 1
442293
}
443294
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
@{
5+
6+
# Script module or binary module file associated with this manifest.
7+
RootModule = 'win_psDscAdapter.psm1'
8+
9+
# Version number of this module.
10+
moduleVersion = '0.0.1'
11+
12+
# ID used to uniquely identify this module
13+
GUID = 'e0dd561d-c47f-4132-aac9-cd9dc8739bb1'
14+
15+
# Author of this module
16+
Author = 'Microsoft Corporation'
17+
18+
# Company or vendor of this module
19+
CompanyName = 'Microsoft Corporation'
20+
21+
# Copyright statement for this module
22+
Copyright = '(c) Microsoft Corporation. All rights reserved.'
23+
24+
# Description of the functionality provided by this module
25+
Description = 'PowerShell Desired State Configuration Module for DSC PowerShell Adapter'
26+
27+
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
28+
FunctionsToExport = @(
29+
'Get-DscResource'
30+
'Invoke-DscResource'
31+
'Invoke-DscCacheRefresh'
32+
)
33+
34+
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
35+
CmdletsToExport = @()
36+
37+
# Variables to export from this module
38+
VariablesToExport = @()
39+
40+
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
41+
AliasesToExport = @()
42+
43+
PrivateData = @{
44+
PSData = @{
45+
ProjectUri = 'https://github.com/PowerShell/dsc'
46+
}
47+
}
48+
}

‎powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

+508
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.