Skip to content

Commit c7eeb0f

Browse files
committed
Fix read-only properties on get operation WMI adapter
1 parent a043eb3 commit c7eeb0f

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

adapters/wmi/Tests/wmi.tests.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,16 @@ Describe 'WMI adapter resource tests' {
108108
$res.afterState.VariableValue | Should -Be 'update'
109109
$res.afterState.UserName | Should -Be ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME)
110110
}
111+
112+
It 'Get works with read-only properties on Win32_ComputerSystem' -Skip:(!$IsWindows) {
113+
$manufacturer = (Get-CimInstance -ClassName Win32_ComputerSystem).Manufacturer
114+
$i = @{
115+
Manufacturer = $manufacturer
116+
} | ConvertTo-Json
117+
118+
$r = dsc resource get -r root.cimv2/Win32_ComputerSystem -i $i
119+
$LASTEXITCODE | Should -Be 0
120+
$res = $r | ConvertFrom-Json
121+
$res.actualState.Manufacturer | Should -Not -BeNullOrEmpty
122+
}
111123
}

adapters/wmi/wmiAdapter.psm1

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,22 +186,26 @@ function GetWmiInstance {
186186
$class = Get-CimClass -Namespace $wmi_namespace -ClassName $wmi_classname -ErrorAction Stop
187187

188188
if ($DesiredState.properties) {
189-
$properties = GetValidCimProperties -CimClass $class -ClassName $wmi_classname -Properties $DesiredState.properties -SkipReadOnly
189+
# For GET operations, we should NOT skip read-only properties since we're just reading them
190+
$properties = GetValidCimProperties -CimClass $class -ClassName $wmi_classname -Properties $DesiredState.properties
190191

191-
$query = BuildWmiQuery -ClassName $wmi_classname -Properties $properties -DesiredStateProperties $DesiredState.properties
192+
# Only build query if we have properties to query
193+
if ($properties -and $properties.Count -gt 0) {
194+
$query = BuildWmiQuery -ClassName $wmi_classname -Properties $properties -DesiredStateProperties $DesiredState.properties
192195

193-
if ($query) {
194-
"Query: $query" | Write-DscTrace -Operation Debug
195-
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -Query $query -ErrorAction Ignore -ErrorVariable err
196+
if ($query) {
197+
"Query: $query" | Write-DscTrace -Operation Debug
198+
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -Query $query -ErrorAction Ignore -ErrorVariable err
196199

197-
if ($null -eq $wmi_instances) {
198-
"No WMI instances found using query '$query'. Retrying with key properties only." | Write-DscTrace -Operation Debug
199-
$keyQuery = BuildWmiQuery -ClassName $wmi_classname -Properties $properties -DesiredStateProperties $DesiredState.properties -KeyPropertiesOnly
200+
if ($null -eq $wmi_instances) {
201+
"No WMI instances found using query '$query'. Retrying with key properties only." | Write-DscTrace -Operation Debug
202+
$keyQuery = BuildWmiQuery -ClassName $wmi_classname -Properties $properties -DesiredStateProperties $DesiredState.properties -KeyPropertiesOnly
200203

201-
if ($keyQuery) {
202-
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -Query $keyQuery -ErrorAction Ignore -ErrorVariable err
203-
if ($null -eq $wmi_instances) {
204-
"No WMI instances found using key properties query '$keyQuery'." | Write-DscTrace -Operation Debug
204+
if ($keyQuery) {
205+
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -Query $keyQuery -ErrorAction Ignore -ErrorVariable err
206+
if ($null -eq $wmi_instances) {
207+
"No WMI instances found using key properties query '$keyQuery'." | Write-DscTrace -Operation Debug
208+
}
205209
}
206210
}
207211
}

0 commit comments

Comments
 (0)