Skip to content

Commit f90b257

Browse files
author
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)
committed
Update WMI adapter to perform query based on provided properties
1 parent cb8d736 commit f90b257

File tree

5 files changed

+96
-25
lines changed

5 files changed

+96
-25
lines changed

dsc/examples/wmi.dsc.yaml

Lines changed: 0 additions & 10 deletions
This file was deleted.

dsc/examples/wmi_inventory.dsc.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
2+
resources:
3+
- name: WMI
4+
type: Microsoft.Windows/WMI
5+
properties:
6+
resources:
7+
- name: computer system
8+
type: root.cimv2/Win32_ComputerSystem
9+
properties:
10+
name:
11+
domain:
12+
totalphysicalmemory:
13+
model:
14+
manufacturer:
15+
- name: operating system
16+
type: root.cimv2/Win32_OperatingSystem
17+
properties:
18+
caption:
19+
version:
20+
osarchitecture:
21+
oslanguage:
22+
- name: system enclosure
23+
type: root.cimv2/Win32_SystemEnclosure
24+
properties:
25+
manufacturer:
26+
model:
27+
serialnumber:
28+
- name: bios
29+
type: root.cimv2/Win32_BIOS
30+
properties:
31+
manufacturer:
32+
version:
33+
serialnumber:
34+
- name: network adapter
35+
type: root.cimv2/Win32_NetworkAdapter
36+
properties:
37+
name:
38+
macaddress:
39+
adaptertype:
40+
netconnectionid:
41+
serviceName:

wmi-adapter/Tests/test_wmi_config.dsc.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ resources:
77
resources:
88
- name: Get OS Info
99
type: root.cimv2/Win32_OperatingSystem
10+
properties:
11+
caption:
12+
version:
13+
osarchitecture:
1014
- name: Get BIOS Info
1115
type: root.cimv2/Win32_BIOS
1216
- name: Get Processor Info

wmi-adapter/Tests/wmi.tests.ps1

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,21 @@ Describe 'WMI adapter resource tests' {
4040
$r = Get-Content -Raw $configPath | dsc config get
4141
$LASTEXITCODE | Should -Be 0
4242
$res = $r | ConvertFrom-Json
43-
$res.results[0].result.actualState[0].LastBootUpTime | Should -Not -BeNull
44-
$res.results[0].result.actualState[1].BiosCharacteristics | Should -Not -BeNull
45-
$res.results[0].result.actualState[2].NumberOfLogicalProcessors | Should -Not -BeNull
43+
$res.results[0].result.actualState[0].LastBootUpTime | Should -BeNullOrEmpty
44+
$res.results[0].result.actualState[0].Caption | Should -Not -BeNullOrEmpty
45+
$res.results[0].result.actualState[0].Version | Should -Not -BeNullOrEmpty
46+
$res.results[0].result.actualState[0].OSArchitecture | Should -Not -BeNullOrEmpty
4647
}
4748

4849
It 'Example config works' -Skip:(!$IsWindows) {
49-
$configPath = Join-Path $PSScriptRoot '..\..\dsc\examples\wmi.dsc.yaml'
50+
$configPath = Join-Path $PSScriptRoot '..\..\dsc\examples\wmi_inventory.dsc.yaml'
5051
$r = dsc config get -p $configPath
5152
$LASTEXITCODE | Should -Be 0
5253
$r | Should -Not -BeNullOrEmpty
5354
$res = $r | ConvertFrom-Json
54-
$res.results[0].result.actualState[0].Model | Should -Not -BeNullOrEmpty
55-
$res.results[0].result.actualState[1].Description | Should -Not -BeNullOrEmpty
55+
$res.results[0].result.actualState[0].Name | Should -Not -BeNullOrEmpty
56+
$res.results[0].result.actualState[0].BootupState | Should -BeNullOrEmpty
57+
$res.results[0].result.actualState[1].Caption | Should -Not -BeNullOrEmpty
58+
$res.results[0].result.actualState[1].BuildNumber | Should -BeNullOrEmpty
5659
}
5760
}

wmi-adapter/wmi.resource.ps1

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ $ProgressPreference = 'Ignore'
1313
$WarningPreference = 'Ignore'
1414
$VerbosePreference = 'Ignore'
1515

16+
function Write-Trace {
17+
param(
18+
[string]$message,
19+
[string]$level = 'Error'
20+
)
21+
22+
$trace = [pscustomobject]@{
23+
$level = $message
24+
} | ConvertTo-Json -Compress
25+
26+
$host.ui.WriteErrorLine($trace)
27+
}
28+
1629
function IsConfiguration($obj) {
1730
if ($null -ne $obj.metadata -and $null -ne $obj.metadata.'Microsoft.DSC' -and $obj.metadata.'Microsoft.DSC'.context -eq 'Configuration') {
1831
return $true
@@ -29,7 +42,6 @@ if ($Operation -eq 'List')
2942
{
3043
$version_string = "";
3144
$author_string = "";
32-
$moduleName = "";
3345

3446
$propertyList = @()
3547
foreach ($p in $r.CimClassProperties)
@@ -79,17 +91,37 @@ elseif ($Operation -eq 'Get')
7991
$wmi_namespace = $type_fields[0].Replace('.','\')
8092
$wmi_classname = $type_fields[1]
8193

82-
#TODO: add filtering based on supplied properties of $r
83-
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -ClassName $wmi_classname
94+
#TODO: identify key properties and add WHERE clause to the query
95+
if ($r.properties)
96+
{
97+
$query = "SELECT $($r.properties.psobject.properties.name -join ',') FROM $wmi_classname"
98+
Write-Trace -Level Trace -message "Query: $query"
99+
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -Query $query
100+
}
101+
else
102+
{
103+
$wmi_instances = Get-CimInstance -Namespace $wmi_namespace -ClassName $wmi_classname
104+
}
84105

85106
if ($wmi_instances)
86107
{
87108
$instance_result = @{}
109+
# TODO: for a `Get`, they key property must be provided so a specific instance is returned rather than just the first
88110
$wmi_instance = $wmi_instances[0] # for 'Get' we return just first matching instance; for 'export' we return all instances
89111
$wmi_instance.psobject.properties | %{
90112
if (($_.Name -ne "type") -and (-not $_.Name.StartsWith("Cim")))
91113
{
92-
$instance_result[$_.Name] = $_.Value
114+
if ($r.properties)
115+
{
116+
if ($r.properties.psobject.properties.name -contains $_.Name)
117+
{
118+
$instance_result[$_.Name] = $_.Value
119+
}
120+
}
121+
else
122+
{
123+
$instance_result[$_.Name] = $_.Value
124+
}
93125
}
94126
}
95127

@@ -98,7 +130,7 @@ elseif ($Operation -eq 'Get')
98130
else
99131
{
100132
$errmsg = "Can not find type " + $r.type + "; please ensure that Get-CimInstance returns this resource type"
101-
Write-Error $errmsg
133+
Write-Trace $errmsg
102134
exit 1
103135
}
104136
}
@@ -114,7 +146,8 @@ elseif ($Operation -eq 'Get')
114146

115147
if ($wmi_instances)
116148
{
117-
$wmi_instance = $wmi_instances[0] # for 'Get' we return just first matching instance; for 'export' we return all instances
149+
# TODO: there's duplicate code here between configuration and non-configuration execution and should be refactored into a helper
150+
$wmi_instance = $wmi_instances[0]
118151
$result = @{}
119152
$wmi_instance.psobject.properties | %{
120153
if (($_.Name -ne "type") -and (-not $_.Name.StartsWith("Cim")))
@@ -126,7 +159,7 @@ elseif ($Operation -eq 'Get')
126159
else
127160
{
128161
$errmsg = "Can not find type " + $inputobj_pscustomobj.type + "; please ensure that Get-CimInstance returns this resource type"
129-
Write-Error $errmsg
162+
Write-Trace $errmsg
130163
exit 1
131164
}
132165
}
@@ -140,5 +173,5 @@ elseif ($Operation -eq 'Validate')
140173
}
141174
else
142175
{
143-
Write-Error "ERROR: Unsupported operation requested from wmigroup.resource.ps1"
144-
}
176+
Write-Trace "ERROR: Unsupported operation requested from wmigroup.resource.ps1"
177+
}

0 commit comments

Comments
 (0)