Skip to content

Commit 1b7ea41

Browse files
authored
Merge pull request #480 from anmenaga/winpsadapter_fix
Fixed WinPSAdapter operations for Binary resources
2 parents 7c55fc7 + 7663c74 commit 1b7ea41

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

powershell-adapter/Tests/win_powershellgroup.tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ Describe 'WindowsPowerShell adapter resource tests' {
4343
$res.actualState.result.properties.DestinationPath | Should -Be "$testFile"
4444
}
4545

46+
It 'Set works on Binary "File" resource' -Skip:(!$IsWindows){
47+
48+
$testFile = "$testdrive\test.txt"
49+
$r = '{"DestinationPath":"' + $testFile.replace('\','\\') + '", type: File, contents: HelloWorld, Ensure: present}' | dsc resource set -r 'PSDesiredStateConfiguration/File'
50+
$LASTEXITCODE | Should -Be 0
51+
Get-Content -Raw -Path $testFile | Should -Be "HelloWorld"
52+
}
53+
4654
It 'Get works on traditional "Script" resource' -Skip:(!$IsWindows){
4755

4856
$testFile = "$testdrive\test.txt"

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -355,15 +355,15 @@ function Invoke-DscOperation {
355355
$invokeResult = Invoke-DscResource -Method $Operation -ModuleName $cachedDscResourceInfo.ModuleName -Name $cachedDscResourceInfo.Name -Property $property
356356

357357
if ($invokeResult.GetType().Name -eq 'Hashtable') {
358-
$invokeResult.keys | ForEach-Object -Begin { $getDscResult = @{} } -Process { $getDscResult[$_] = $invokeResult.$_ }
358+
$invokeResult.keys | ForEach-Object -Begin { $ResultProperties = @{} } -Process { $ResultProperties[$_] = $invokeResult.$_ }
359359
}
360360
else {
361361
# the object returned by WMI is a CIM instance with a lot of additional data. only return DSC properties
362-
$invokeResult.psobject.Properties.name | Where-Object { 'CimClass', 'CimInstanceProperties', 'CimSystemProperties' -notcontains $_ } | ForEach-Object -Begin { $getDscResult = @{} } -Process { $getDscResult[$_] = $invokeResult.$_ }
362+
$invokeResult.psobject.Properties.name | Where-Object { 'CimClass', 'CimInstanceProperties', 'CimSystemProperties' -notcontains $_ } | ForEach-Object -Begin { $ResultProperties = @{} } -Process { $ResultProperties[$_] = $invokeResult.$_ }
363363
}
364364

365365
# set the properties of the OUTPUT object from the result of Get-TargetResource
366-
$addToActualState.properties = $getDscResult
366+
$addToActualState.properties = $ResultProperties
367367
}
368368
catch {
369369
'ERROR: ' + $_.Exception.Message | Write-DscTrace
@@ -422,21 +422,19 @@ function Invoke-DscOperation {
422422

423423
# morph the INPUT object into a hashtable named "property" for the cmdlet Invoke-DscResource
424424
$DesiredState.properties.psobject.properties | ForEach-Object -Begin { $property = @{} } -Process { $property[$_.Name] = $_.Value }
425-
426425
# using the cmdlet from PSDesiredStateConfiguration module in Windows
427426
try {
428-
$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 )
429-
430-
if ($getResult.GetType().Name -eq 'Hashtable') {
431-
$getResult.keys | ForEach-Object -Begin { $getDscResult = @{} } -Process { $getDscResult[$_] = $getResult.$_ }
427+
$invokeResult = Invoke-DscResource -Method $Operation -ModuleName $cachedDscResourceInfo.ModuleName -Name $cachedDscResourceInfo.Name -Property $property
428+
if ($invokeResult.GetType().Name -eq 'Hashtable') {
429+
$invokeResult.keys | ForEach-Object -Begin { $ResultProperties = @{} } -Process { $ResultProperties[$_] = $invokeResult.$_ }
432430
}
433431
else {
434432
# the object returned by WMI is a CIM instance with a lot of additional data. only return DSC properties
435-
$getResult.psobject.Properties.name | Where-Object { 'CimClass', 'CimInstanceProperties', 'CimSystemProperties' -notcontains $_ } | ForEach-Object -Begin { $getDscResult = @{} } -Process { $getDscResult[$_] = $getResult.$_ }
433+
$invokeResult.psobject.Properties.name | Where-Object { 'CimClass', 'CimInstanceProperties', 'CimSystemProperties' -notcontains $_ } | ForEach-Object -Begin { $ResultProperties = @{} } -Process { $ResultProperties[$_] = $invokeResult.$_ }
436434
}
437435

438436
# set the properties of the OUTPUT object from the result of Get-TargetResource
439-
$addToActualState.properties = $getDscResult
437+
$addToActualState.properties = $ResultProperties
440438
}
441439
catch {
442440
'ERROR: ' + $_.Exception.Message | Write-DscTrace

0 commit comments

Comments
 (0)