@@ -13,6 +13,19 @@ $ProgressPreference = 'Ignore'
13
13
$WarningPreference = ' Ignore'
14
14
$VerbosePreference = ' Ignore'
15
15
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
+
16
29
function IsConfiguration ($obj ) {
17
30
if ($null -ne $obj.metadata -and $null -ne $obj.metadata .' Microsoft.DSC' -and $obj.metadata .' Microsoft.DSC' .context -eq ' Configuration' ) {
18
31
return $true
@@ -29,7 +42,6 @@ if ($Operation -eq 'List')
29
42
{
30
43
$version_string = " " ;
31
44
$author_string = " " ;
32
- $moduleName = " " ;
33
45
34
46
$propertyList = @ ()
35
47
foreach ($p in $r.CimClassProperties )
@@ -79,17 +91,37 @@ elseif ($Operation -eq 'Get')
79
91
$wmi_namespace = $type_fields [0 ].Replace(' .' , ' \' )
80
92
$wmi_classname = $type_fields [1 ]
81
93
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
+ }
84
105
85
106
if ($wmi_instances )
86
107
{
87
108
$instance_result = @ {}
109
+ # TODO: for a `Get`, they key property must be provided so a specific instance is returned rather than just the first
88
110
$wmi_instance = $wmi_instances [0 ] # for 'Get' we return just first matching instance; for 'export' we return all instances
89
111
$wmi_instance.psobject.properties | % {
90
112
if (($_.Name -ne " type" ) -and (-not $_.Name.StartsWith (" Cim" )))
91
113
{
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
+ }
93
125
}
94
126
}
95
127
@@ -98,7 +130,7 @@ elseif ($Operation -eq 'Get')
98
130
else
99
131
{
100
132
$errmsg = " Can not find type " + $r.type + " ; please ensure that Get-CimInstance returns this resource type"
101
- Write-Error $errmsg
133
+ Write-Trace $errmsg
102
134
exit 1
103
135
}
104
136
}
@@ -114,7 +146,8 @@ elseif ($Operation -eq 'Get')
114
146
115
147
if ($wmi_instances )
116
148
{
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 ]
118
151
$result = @ {}
119
152
$wmi_instance.psobject.properties | % {
120
153
if (($_.Name -ne " type" ) -and (-not $_.Name.StartsWith (" Cim" )))
@@ -126,7 +159,7 @@ elseif ($Operation -eq 'Get')
126
159
else
127
160
{
128
161
$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
130
163
exit 1
131
164
}
132
165
}
@@ -140,5 +173,5 @@ elseif ($Operation -eq 'Validate')
140
173
}
141
174
else
142
175
{
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