Skip to content

Add WMI set capability #946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion wmi-adapter/Tests/wmi.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,60 @@ Describe 'WMI adapter resource tests' {
$LASTEXITCODE | Should -Be 0
$r | Should -Not -BeNullOrEmpty
$res = $r | ConvertFrom-Json

$res.results[1].result.actualState.result[0].properties.Name | Should -Not -BeNullOrEmpty
$res.results[1].result.actualState.result[0].properties.BootupState | Should -BeNullOrEmpty
$res.results[1].result.actualState.result[1].properties.Caption | Should -Not -BeNullOrEmpty
$res.results[1].result.actualState.result[1].properties.BuildNumber | Should -BeNullOrEmpty
$res.results[1].result.actualState.result[4].properties.AdapterType | Should -BeLike "Ethernet*"
$res.results[1].result.actualState.result[4].properties.Name | Should -Not -BeNullOrEmpty
}

It 'Set does not work without input for resource' -Skip:(!$IsWindows) {
$out = dsc resource set --resource root.cimv2/Win32_Environment --input '{}' 2>$TestDrive/error.log
$out | Should -BeNullOrEmpty
(Get-Content $TestDrive/error.log -Raw) | Should -BeLike "*No valid properties found in the CIM class 'Win32_Environment' for the provided properties.*"
}

It 'Set does not work without a key property' -Skip:(!$IsWindows) {
$i = @{
VariableValue = "TestValue"
UserName = ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME) # Read-only property is key, but we require a key property to be set
} | ConvertTo-Json

$out = dsc resource set -r root.cimv2/Win32_Environment -i $i 2>$TestDrive/error2.log
$out | Should -BeNullOrEmpty
(Get-Content $TestDrive/error2.log -Raw) | Should -BeLike "*All properties specified in the CIM class 'Win32_Environment' are read-only, which is not supported.*"
}

It 'Set works on a WMI resource' -Skip:(!$IsWindows) {
$i = @{
UserName = ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME) # Read-only key property required
Name = 'test'
VariableValue = 'test'
} | ConvertTo-Json

$r = dsc resource set -r root.cimv2/Win32_Environment -i $i
$LASTEXITCODE | Should -Be 0

$res = $r | ConvertFrom-Json
$res.afterState.Name | Should -Be 'test'
$res.afterState.VariableValue | Should -Be 'test'
$res.afterState.UserName | Should -Be ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME)
}

It 'Update works on a WMI resource' -Skip:(!$IsWindows) {
$i = @{
UserName = ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME) # Read-only key property required
Name = 'test'
VariableValue = 'update'
} | ConvertTo-Json

$r = dsc resource set -r root.cimv2/Win32_Environment -i $i
$LASTEXITCODE | Should -Be 0

$res = $r | ConvertFrom-Json
$res.afterState.Name | Should -Be 'test'
$res.afterState.VariableValue | Should -Be 'update'
$res.afterState.UserName | Should -Be ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME)
}
}
14 changes: 14 additions & 0 deletions wmi-adapter/wmi.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@
],
"input": "stdin"
},
"set": {
"executable": "powershell",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-Command",
"$Input | ./wmi.resource.ps1 Set"
],
"input": "stdin",
"implementsPretest": false
},
"validate": {
"executable": "powershell",
"args": [
Expand Down
2 changes: 1 addition & 1 deletion wmi-adapter/wmi.resource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ param(
)

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

if ('Validate' -ne $Operation) {
# initialize OUTPUT as array
Expand Down
Loading
Loading