9
9
$stdinput
10
10
)
11
11
12
+ # catch any un-caught exception and write it to the error stream
13
+ trap {
14
+ Write-Trace - Level Error - message $_.Exception.Message
15
+ exit 1
16
+ }
17
+
12
18
$ProgressPreference = ' Ignore'
13
19
$WarningPreference = ' Ignore'
14
20
$VerbosePreference = ' Ignore'
15
21
22
+ function Write-Trace {
23
+ param (
24
+ [string ]$message ,
25
+ [string ]$level = ' Error'
26
+ )
27
+
28
+ $trace = [pscustomobject ]@ {
29
+ $level = $message
30
+ } | ConvertTo-Json - Compress
31
+
32
+ $host.ui.WriteErrorLine ($trace )
33
+ }
34
+
16
35
function IsConfiguration ($obj ) {
17
36
if ($null -ne $obj.metadata -and $null -ne $obj.metadata .' Microsoft.DSC' -and $obj.metadata .' Microsoft.DSC' .context -eq ' Configuration' ) {
18
37
return $true
@@ -29,7 +48,6 @@ if ($Operation -eq 'List')
29
48
{
30
49
$version_string = " " ;
31
50
$author_string = " " ;
32
- $moduleName = " " ;
33
51
34
52
$propertyList = @ ()
35
53
foreach ($p in $r.CimClassProperties )
@@ -79,17 +97,69 @@ elseif ($Operation -eq 'Get')
79
97
$wmi_namespace = $type_fields [0 ].Replace(' .' , ' \' )
80
98
$wmi_classname = $type_fields [1 ]
81
99
82
- # TODO: add filtering based on supplied properties of $r
83
- $wmi_instances = Get-CimInstance - Namespace $wmi_namespace - ClassName $wmi_classname
100
+ # TODO: identify key properties and add WHERE clause to the query
101
+ if ($r.properties )
102
+ {
103
+ $query = " SELECT $ ( $r.properties.psobject.properties.name -join ' ,' ) FROM $wmi_classname "
104
+ $where = " WHERE "
105
+ $useWhere = $false
106
+ $first = $true
107
+ foreach ($property in $r.properties.psobject.properties )
108
+ {
109
+ # TODO: validate property against the CIM class to give better error message
110
+ if ($null -ne $property.value )
111
+ {
112
+ $useWhere = $true
113
+ if ($first )
114
+ {
115
+ $first = $false
116
+ }
117
+ else
118
+ {
119
+ $where += " AND "
120
+ }
121
+
122
+ if ($property.TypeNameOfValue -eq " System.String" )
123
+ {
124
+ $where += " $ ( $property.Name ) = '$ ( $property.Value ) '"
125
+ }
126
+ else
127
+ {
128
+ $where += " $ ( $property.Name ) = $ ( $property.Value ) "
129
+ }
130
+ }
131
+ }
132
+ if ($useWhere )
133
+ {
134
+ $query += $where
135
+ }
136
+ Write-Trace - Level Trace - message " Query: $query "
137
+ $wmi_instances = Get-CimInstance - Namespace $wmi_namespace - Query $query - ErrorAction Stop
138
+ }
139
+ else
140
+ {
141
+ $wmi_instances = Get-CimInstance - Namespace $wmi_namespace - ClassName $wmi_classname - ErrorAction Stop
142
+ }
84
143
85
144
if ($wmi_instances )
86
145
{
87
146
$instance_result = @ {}
147
+ # TODO: for a `Get`, they key property must be provided so a specific instance is returned rather than just the first
88
148
$wmi_instance = $wmi_instances [0 ] # for 'Get' we return just first matching instance; for 'export' we return all instances
89
149
$wmi_instance.psobject.properties | % {
90
150
if (($_.Name -ne " type" ) -and (-not $_.Name.StartsWith (" Cim" )))
91
151
{
92
- $instance_result [$_.Name ] = $_.Value
152
+ if ($r.properties )
153
+ {
154
+ if ($r.properties.psobject.properties.name -contains $_.Name )
155
+ {
156
+ $instance_result [$_.Name ] = $_.Value
157
+ }
158
+ }
159
+ else
160
+ {
161
+ $instance_result [$_.Name ] = $_.Value
162
+ }
93
163
}
94
164
}
95
165
@@ -98,7 +168,7 @@ elseif ($Operation -eq 'Get')
98
168
else
99
169
{
100
170
$errmsg = " Can not find type " + $r.type + " ; please ensure that Get-CimInstance returns this resource type"
101
- Write-Error $errmsg
171
+ Write-Trace $errmsg
102
172
exit 1
103
173
}
104
174
}
@@ -110,11 +180,12 @@ elseif ($Operation -eq 'Get')
110
180
$wmi_classname = $type_fields [1 ]
111
181
112
182
# TODO: add filtering based on supplied properties of $inputobj_pscustomobj
113
- $wmi_instances = Get-CimInstance - Namespace $wmi_namespace - ClassName $wmi_classname
183
+ $wmi_instances = Get-CimInstance - Namespace $wmi_namespace - ClassName $wmi_classname - ErrorAction Stop
114
184
115
185
if ($wmi_instances )
116
186
{
117
- $wmi_instance = $wmi_instances [0 ] # for 'Get' we return just first matching instance; for 'export' we return all instances
187
+ # TODO: there's duplicate code here between configuration and non-configuration execution and should be refactored into a helper
188
+ $wmi_instance = $wmi_instances [0 ]
118
189
$result = @ {}
119
190
$wmi_instance.psobject.properties | % {
120
191
if (($_.Name -ne " type" ) -and (-not $_.Name.StartsWith (" Cim" )))
@@ -126,7 +197,7 @@ elseif ($Operation -eq 'Get')
126
197
else
127
198
{
128
199
$errmsg = " Can not find type " + $inputobj_pscustomobj.type + " ; please ensure that Get-CimInstance returns this resource type"
129
- Write-Error $errmsg
200
+ Write-Trace $errmsg
130
201
exit 1
131
202
}
132
203
}
@@ -140,5 +211,5 @@ elseif ($Operation -eq 'Validate')
140
211
}
141
212
else
142
213
{
143
- Write-Error " ERROR: Unsupported operation requested from wmigroup.resource.ps1"
144
- }
214
+ Write-Trace " ERROR: Unsupported operation requested from wmigroup.resource.ps1"
215
+ }
0 commit comments