Skip to content

Commit dcf9e51

Browse files
authored
Merge pull request #946 from Gijsreyn/wmi-set-operation
Add WMI set capability
2 parents b880b09 + 1a77e80 commit dcf9e51

File tree

4 files changed

+316
-52
lines changed

4 files changed

+316
-52
lines changed

wmi-adapter/Tests/wmi.tests.ps1

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,60 @@ Describe 'WMI adapter resource tests' {
5252
$LASTEXITCODE | Should -Be 0
5353
$r | Should -Not -BeNullOrEmpty
5454
$res = $r | ConvertFrom-Json
55+
5556
$res.results[1].result.actualState.result[0].properties.Name | Should -Not -BeNullOrEmpty
5657
$res.results[1].result.actualState.result[0].properties.BootupState | Should -BeNullOrEmpty
5758
$res.results[1].result.actualState.result[1].properties.Caption | Should -Not -BeNullOrEmpty
5859
$res.results[1].result.actualState.result[1].properties.BuildNumber | Should -BeNullOrEmpty
59-
$res.results[1].result.actualState.result[4].properties.AdapterType | Should -BeLike "Ethernet*"
60+
$res.results[1].result.actualState.result[4].properties.Name | Should -Not -BeNullOrEmpty
61+
}
62+
63+
It 'Set does not work without input for resource' -Skip:(!$IsWindows) {
64+
$out = dsc resource set --resource root.cimv2/Win32_Environment --input '{}' 2>$TestDrive/error.log
65+
$out | Should -BeNullOrEmpty
66+
(Get-Content $TestDrive/error.log -Raw) | Should -BeLike "*No valid properties found in the CIM class 'Win32_Environment' for the provided properties.*"
67+
}
68+
69+
It 'Set does not work without a key property' -Skip:(!$IsWindows) {
70+
$i = @{
71+
VariableValue = "TestValue"
72+
UserName = ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME) # Read-only property is key, but we require a key property to be set
73+
} | ConvertTo-Json
74+
75+
$out = dsc resource set -r root.cimv2/Win32_Environment -i $i 2>$TestDrive/error2.log
76+
$out | Should -BeNullOrEmpty
77+
(Get-Content $TestDrive/error2.log -Raw) | Should -BeLike "*All properties specified in the CIM class 'Win32_Environment' are read-only, which is not supported.*"
78+
}
79+
80+
It 'Set works on a WMI resource' -Skip:(!$IsWindows) {
81+
$i = @{
82+
UserName = ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME) # Read-only key property required
83+
Name = 'test'
84+
VariableValue = 'test'
85+
} | ConvertTo-Json
86+
87+
$r = dsc resource set -r root.cimv2/Win32_Environment -i $i
88+
$LASTEXITCODE | Should -Be 0
89+
90+
$res = $r | ConvertFrom-Json
91+
$res.afterState.Name | Should -Be 'test'
92+
$res.afterState.VariableValue | Should -Be 'test'
93+
$res.afterState.UserName | Should -Be ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME)
94+
}
95+
96+
It 'Update works on a WMI resource' -Skip:(!$IsWindows) {
97+
$i = @{
98+
UserName = ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME) # Read-only key property required
99+
Name = 'test'
100+
VariableValue = 'update'
101+
} | ConvertTo-Json
102+
103+
$r = dsc resource set -r root.cimv2/Win32_Environment -i $i
104+
$LASTEXITCODE | Should -Be 0
105+
106+
$res = $r | ConvertFrom-Json
107+
$res.afterState.Name | Should -Be 'test'
108+
$res.afterState.VariableValue | Should -Be 'update'
109+
$res.afterState.UserName | Should -Be ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME)
60110
}
61111
}

wmi-adapter/wmi.dsc.resource.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@
3535
],
3636
"input": "stdin"
3737
},
38+
"set": {
39+
"executable": "powershell",
40+
"args": [
41+
"-NoLogo",
42+
"-NonInteractive",
43+
"-NoProfile",
44+
"-ExecutionPolicy",
45+
"Bypass",
46+
"-Command",
47+
"$Input | ./wmi.resource.ps1 Set"
48+
],
49+
"input": "stdin",
50+
"implementsPretest": false
51+
},
3852
"validate": {
3953
"executable": "powershell",
4054
"args": [

wmi-adapter/wmi.resource.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ param(
1010
)
1111

1212
# Import private functions
13-
$wmiAdapter = Import-Module "$PSScriptRoot/wmiAdapter.psm1" -Force -PassThru
13+
$wmiAdapter = Import-Module "$PSScriptRoot\wmiAdapter.psm1" -Force -PassThru
1414

1515
if ('Validate' -ne $Operation) {
1616
# initialize OUTPUT as array

0 commit comments

Comments
 (0)