Skip to content
Open
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
12 changes: 12 additions & 0 deletions adapters/wmi/Tests/wmi.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,16 @@ Describe 'WMI adapter resource tests' {
$res.afterState.VariableValue | Should -Be 'update'
$res.afterState.UserName | Should -Be ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME)
}

It 'Get works with read-only properties on Win32_ComputerSystem' -Skip:(!$IsWindows) {
$manufacturer = (Get-CimInstance -ClassName Win32_ComputerSystem).Manufacturer
$i = @{
Manufacturer = $manufacturer
} | ConvertTo-Json

$r = dsc resource get -r root.cimv2/Win32_ComputerSystem -i $i
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.Manufacturer | Should -Not -BeNullOrEmpty
}
}
28 changes: 16 additions & 12 deletions adapters/wmi/wmiAdapter.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -186,22 +186,26 @@ function GetWmiInstance {
$class = Get-CimClass -Namespace $wmi_namespace -ClassName $wmi_classname -ErrorAction Stop

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

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

if ($query) {
"Query: $query" | Write-DscTrace -Operation Debug
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -Query $query -ErrorAction Ignore -ErrorVariable err
if ($query) {
"Query: $query" | Write-DscTrace -Operation Debug
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -Query $query -ErrorAction Ignore -ErrorVariable err

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

if ($keyQuery) {
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -Query $keyQuery -ErrorAction Ignore -ErrorVariable err
if ($null -eq $wmi_instances) {
"No WMI instances found using key properties query '$keyQuery'." | Write-DscTrace -Operation Debug
if ($keyQuery) {
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -Query $keyQuery -ErrorAction Ignore -ErrorVariable err
if ($null -eq $wmi_instances) {
"No WMI instances found using key properties query '$keyQuery'." | Write-DscTrace -Operation Debug
}
}
}
}
Expand Down
Loading