@@ -32,14 +32,6 @@ function Write-Trace {
32
32
$host.ui.WriteErrorLine ($trace )
33
33
}
34
34
35
- function IsConfiguration ($obj ) {
36
- if ($null -ne $obj.metadata -and $null -ne $obj.metadata .' Microsoft.DSC' -and $obj.metadata .' Microsoft.DSC' .context -eq ' configuration' ) {
37
- return $true
38
- }
39
-
40
- return $false
41
- }
42
-
43
35
if ($Operation -eq ' List' )
44
36
{
45
37
$clases = Get-CimClass
@@ -89,89 +81,81 @@ elseif ($Operation -eq 'Get')
89
81
90
82
$result = @ ()
91
83
92
- if (IsConfiguration $inputobj_pscustomobj ) # we are processing a config batch
84
+ foreach ( $r in $inputobj_pscustomobj.resources )
93
85
{
94
- foreach ($r in $inputobj_pscustomobj.resources )
95
- {
96
- $type_fields = $r.type -split " /"
97
- $wmi_namespace = $type_fields [0 ].Replace(' .' , ' \' )
98
- $wmi_classname = $type_fields [1 ]
86
+ $type_fields = $r.type -split " /"
87
+ $wmi_namespace = $type_fields [0 ].Replace(' .' , ' \' )
88
+ $wmi_classname = $type_fields [1 ]
99
89
100
- # TODO: identify key properties and add WHERE clause to the query
101
- if ($r.properties )
90
+ # TODO: identify key properties and add WHERE clause to the query
91
+ if ($r.properties )
92
+ {
93
+ $query = " SELECT $ ( $r.properties.psobject.properties.name -join ' ,' ) FROM $wmi_classname "
94
+ $where = " WHERE "
95
+ $useWhere = $false
96
+ $first = $true
97
+ foreach ($property in $r.properties.psobject.properties )
102
98
{
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 )
99
+ # TODO: validate property against the CIM class to give better error message
100
+ if ($null -ne $property.value )
108
101
{
109
- # TODO: validate property against the CIM class to give better error message
110
- if ($null -ne $property .value )
102
+ $useWhere = $true
103
+ if ($first )
111
104
{
112
- $useWhere = $true
113
- if ($first )
114
- {
115
- $first = $false
116
- }
117
- else
118
- {
119
- $where += " AND "
120
- }
105
+ $first = $false
106
+ }
107
+ else
108
+ {
109
+ $where += " AND "
110
+ }
121
111
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
- }
112
+ if ($property.TypeNameOfValue -eq " System.String" )
113
+ {
114
+ $where += " $ ( $property.Name ) = '$ ( $property.Value ) '"
115
+ }
116
+ else
117
+ {
118
+ $where += " $ ( $property.Name ) = $ ( $property.Value ) "
130
119
}
131
120
}
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
121
}
139
- else
122
+ if ( $useWhere )
140
123
{
141
- $wmi_instances = Get-CimInstance - Namespace $wmi_namespace - ClassName $wmi_classname - ErrorAction Stop
124
+ $query += $where
142
125
}
126
+ Write-Trace - Level Trace - message " Query: $query "
127
+ $wmi_instances = Get-CimInstance - Namespace $wmi_namespace - Query $query - ErrorAction Stop
128
+ }
129
+ else
130
+ {
131
+ $wmi_instances = Get-CimInstance - Namespace $wmi_namespace - ClassName $wmi_classname - ErrorAction Stop
132
+ }
143
133
144
- if ($wmi_instances )
145
- {
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
148
- $wmi_instance = $wmi_instances [0 ] # for 'Get' we return just first matching instance; for 'export' we return all instances
149
- $wmi_instance.psobject.properties | % {
150
- if (($_.Name -ne " type" ) -and (-not $_.Name.StartsWith (" Cim" )))
134
+ if ($wmi_instances )
135
+ {
136
+ $instance_result = @ {}
137
+ # TODO: for a `Get`, they key property must be provided so a specific instance is returned rather than just the first
138
+ $wmi_instance = $wmi_instances [0 ] # for 'Get' we return just first matching instance; for 'export' we return all instances
139
+ $wmi_instance.psobject.properties | % {
140
+ if (($_.Name -ne " type" ) -and (-not $_.Name.StartsWith (" Cim" )))
141
+ {
142
+ if ($r.properties )
151
143
{
152
- if ($r.properties )
153
- {
154
- if ($r.properties.psobject.properties.name -contains $_.Name )
155
- {
156
- $instance_result [$_.Name ] = $_.Value
157
- }
158
- }
159
- else
144
+ if ($r.properties.psobject.properties.name -contains $_.Name )
160
145
{
161
146
$instance_result [$_.Name ] = $_.Value
162
147
}
163
148
}
149
+ else
150
+ {
151
+ $instance_result [$_.Name ] = $_.Value
152
+ }
164
153
}
165
-
166
- $result += @ ($instance_result )
167
154
}
155
+
156
+ $result += @ ($instance_result )
168
157
}
169
158
}
170
- else
171
- {
172
- Write-Trace - Level Error - message " Operation requires a configuration object as input"
173
- exit 1
174
- }
175
159
176
160
$result | ConvertTo-Json - Compress
177
161
}
0 commit comments