Skip to content

Commit a735c6d

Browse files
committed
Fix issues with PowerShell adapter tests and module imports
1 parent 9758b1f commit a735c6d

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,22 @@ Describe 'PowerShell adapter resource tests' {
2828
($resources | ? {$_.Type -eq 'PSDesiredStateConfiguration/File'}).Count | Should -Be 1
2929
}
3030

31-
It 'Get works on Binary resource' -Skip:(!$IsWindows){
31+
It 'Get works on Binary "File" resource' -Skip:(!$IsWindows){
3232

33-
$r = '{"Name": "File test", "Type":"PSDesiredStateConfiguration/File", "DestinationPath":"$env:TEMP\\test.txt"}' | dsc resource get -r 'Microsoft.Dsc/WindowsPowerShell'
33+
$r = '{"DestinationPath":"$env:TEMP\\test.txt"}' | dsc resource get -r 'PSDesiredStateConfiguration/File'
3434
$LASTEXITCODE | Should -Be 0
3535
$res = $r | ConvertFrom-Json
3636
$res.actualState.result.properties.Contents | Should -BeNullOrEmpty
3737
}
3838

39+
It 'Get works on traditional "Script" resource' -Skip:(!$IsWindows){
40+
41+
$r = '{"GetScript": "Get-Content $env:TEMP\\tests.txt", "SetScript": "throw", "TestScript": "throw"}' | dsc resource get -r 'PSDesiredStateConfiguration/Script'
42+
$LASTEXITCODE | Should -Be 0
43+
$res = $r | ConvertFrom-Json
44+
$res.actualState.result.properties.GetScript | Should -BeNullOrEmpty
45+
}
46+
3947
It 'Get works on class-based resource' -Skip:(!$IsWindows){
4048

4149
$r = "{'Name':'TestClassResource1', 'Type':'TestClassResource/TestClassResource'}" | dsc resource get -r 'Microsoft.Dsc/PowerShell'

powershell-adapter/psDscAdapter/psDscAdapter.psm1

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function Invoke-DscCacheRefresh {
5555
}
5656
elseif ('PSDesiredStateConfiguration' -eq $module) {
5757
# workaround: the binary modules don't have a module name, so we have to special case File and SignatureValidation resources that ship in Windows
58-
$DscResources = Get-DscResource | Where-Object { $_.modulename -eq 'PSDesiredStateConfiguration' -or ( $_.modulename -eq $null -and $_.parentpath -like "$env:windir\System32\Configuration\*" )}
58+
$DscResources = Get-DscResource | Where-Object { $_.modulename -eq 'PSDesiredStateConfiguration' -or ( $_.modulename -eq $null -and $_.parentpath -like "$env:windir\System32\Configuration\*" ) }
5959
}
6060
else {
6161
# if no module is specified, get all resources
@@ -242,9 +242,14 @@ function Get-ActualState {
242242
try {
243243
$getResult = Invoke-DscResource -Method Get -ModuleName $cachedDscResourceInfo.ModuleName -Name $cachedDscResourceInfo.Name -Property $property
244244

245-
# only return DSC properties
246-
$getResult.psobject.Properties.name | Where-Object { 'CimClass','CimInstanceProperties','CimSystemProperties' -notcontains $_ } | ForEach-Object -Begin { $getDscResult = @{} } -Process { $getDscResult[$_] = $getResult.$_ }
247-
245+
if ($getResult.GetType().Name -eq 'Hashtable') {
246+
$getResult.keys | ForEach-Object -Begin { $getDscResult = @{} } -Process { $getDscResult[$_] = $getResult.$_ }
247+
}
248+
else {
249+
# the object returned by WMI is a CIM instance with a lot of additional data. only return DSC properties
250+
$getResult.psobject.Properties.name | Where-Object { 'CimClass', 'CimInstanceProperties', 'CimSystemProperties' -notcontains $_ } | ForEach-Object -Begin { $getDscResult = @{} } -Process { $getDscResult[$_] = $getResult.$_ }
251+
}
252+
248253
# set the properties of the OUTPUT object from the result of Get-TargetResource
249254
$addToActualState.properties = $getDscResult
250255
}
@@ -296,9 +301,14 @@ function Get-ActualState {
296301
try {
297302
$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 )
298303

299-
# only return DSC properties
300-
$getResult.psobject.Properties.name | Where-Object { 'CimClass','CimInstanceProperties','CimSystemProperties' -notcontains $_ } | ForEach-Object -Begin { $getDscResult = @{} } -Process { $getDscResult[$_] = $getResult.$_ }
301-
304+
if ($getResult.GetType().Name -eq 'Hashtable') {
305+
$getResult.keys | ForEach-Object -Begin { $getDscResult = @{} } -Process { $getDscResult[$_] = $getResult.$_ }
306+
}
307+
else {
308+
# the object returned by WMI is a CIM instance with a lot of additional data. only return DSC properties
309+
$getResult.psobject.Properties.name | Where-Object { 'CimClass', 'CimInstanceProperties', 'CimSystemProperties' -notcontains $_ } | ForEach-Object -Begin { $getDscResult = @{} } -Process { $getDscResult[$_] = $getResult.$_ }
310+
}
311+
302312
# set the properties of the OUTPUT object from the result of Get-TargetResource
303313
$addToActualState.properties = $getDscResult
304314
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$env:psmodulepath = $env:psmodulepath.trimend(';')

0 commit comments

Comments
 (0)