Search-WMI.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
function Search-Wmi
{
    <#
 .Synopsis
  Searches the WMI repository
 .Description
  Searches the help metadata that is built into the WMI repository
  to find WMI classes to fit a particular need. The search can
  look for:
   - Information by class name
   - Keywords in the description text
   - Keywords in property names or descriptions
   - Keywords in method names or descriptions            
 .Example
        # Get all class information from root\cimv2 namespace
        Search-WMI
    .Example
        #
        Search-Wmi -ForName *registry* -Namespace root\default
    .Example
        # Save Everything in Root Default
        Search-Wmi -Namespace root\default | Export-Clixml .\root.default.clixml
 #>

    param(
    # The name of the class you'd like to search for. By default, *
    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [String]$ForName = "*",


    # If provided, will search for specific text in a class description
    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [String]$ForTextInDescription,
    
    # If provided, will look for specific property names on a wmi class
    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [string]$ForAPropertyLike,
    # If set, will return properties that can write
    [switch]$OnlyIfThePropertyCanWrite,
    # If provided, will look for specific method names on a wmi class
    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [string]$ForAMethodLike,
    # The namespace you'd like to search in, by default root\CIMv2
    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [string]$Namespace = "root\cimv2",
    # If set, will only return wmi types that can be created
    [switch]$OnlyIfItCanBeCreated,
    # If set, will only return WMI types that can be deleted
    [switch]$OnlyIfItCanBeDeleted,
    # If set, will only return WMI types that can be updated
    [switch]$OnlyIfItCanBeUpdated,        
    # The name of the computer to get WMI information from
    [string]$ComputerName,
    # The credential used to connect to the computer
    [Management.Automation.PSCredential]$Credential,
    # If set, will search recursively
    [Switch]$Recurse,
    # If set, will only search for WMI event classes
    [Switch]$OnlySearchForAnEvent
    )
    
    
    process {
        Get-WmiObject $ForName -Namespace $namespace -Recurse:$Recurse -Amended -List | 
            Select-Object @{
                    Name='Class'
                    Expression={
                        Write-Progress "Getting WMI Data" " $($_.Name)"
                        $_.Name
                    
                    }                
                }, @{
                    Name='Namespace'
                    Expression={
                        $_.__Namespace 
                    }
                }, @{
                    Name='Description'
                    Expression={
                        
                        $_.Qualifiers.Item("Description").Value
                    }
                },@{
                    Name='CanCreate'
                    Expression={
                        try {
                            [bool](($_.Qualifiers.Item('SupportsCreate').Value))
                        } catch {
                            $false 
                        }                    
                    }
                }, @{
                    Name='CanUpdate'
                    Expression={
                        try {
                            [bool](($_.Qualifiers.Item('SupportsUpdate').Value))
                        } catch {
                            $false
                        }}                
                }, @{
                    Name='CanDelete'
                    Expression={
                        try {
                            [bool]($_.Qualifiers.Item('SupportsDelete').Value)
                        } catch {
                            $false
                        }
                        
                    }
                }, @{
                    Name='IsAbstract'
                    Expression={
                        try {
                            [bool]($_.Qualifiers.Item('Abstract').Value)
                        } catch {
                            $false
                        }                        
                    }
                },@{
                    Name='IsSingleton'
                    Expression={
                        try {
                            [bool]$_.Qualifiers.Item('Singleton').Value                
                        } catch {
                            $false
                        }
                        
                    }
                },@{
                    Name='IsAssociation'
                    Expression={
                        try {
                            [bool]$_.Qualifiers.Item('Association').Value                
                        } catch {
                            $false
                        }
                        
                    }
                },@{
                    Name='Provider'
                    Expression={
                        try {
                            $_.Qualifiers.Item('Provider').Value                
                        } catch {
                            ""
                        }
                        
                    }
                },@{
                    Label='Privileges'
                    Expression = { 
                        try {
                            $_.Qualifiers.Item("Privileges").Value -join ','                                        
                        } catch {
                            ""
                        }
                    }
                },@{
                    Name='Properties'
                    Expression={
                        $_.Properties | 
                            Select-Object @{
                                Label='PropertyName'
                                Expression={$_.Name}
                            }, @{
                                Label='PropertyType'
                                Expression={$_.Type}
                            }, @{
                                Label='Description'
                                Expression={$_.Qualifiers.Item("Description").Value}
                            }, @{
                                Label='CanRead'
                                Expression={$_.Qualifiers.Item("Read").Value}
                            }, @{
                                Label='CanWrite'
                                Expression={
                                    try {
                                        $_.Qualifiers.Item("Write").Value
                                    } catch {
                                        $false
                                    }
                                }
                            }, @{
                                Label='References'
                                Expression = { 
                                    try {
                                        $v = $_.Qualifiers.Item("CimType").Value
                                        if ($v -like "ref:*") {
                                            $v.Substring(4)
                                        } else {
                                            $null
                                        }
                                    } catch {
                                        $false
                                    }
                                }
                            }, @{
                                Label='MapsTo'
                                Expression = { 
                                    try {
                                        $_.Qualifiers.Item("MappingStrings").Value
                                        
                                    } catch {
                                        ""
                                    }
                                }
                            }, @{
                                Label='Privileges'
                                Expression = { 
                                    try {
                                        $_.Qualifiers.Item("Privileges").Value -join ','                                        
                                    } catch {
                                        ""
                                    }
                                }
                            }, @{
                                Label='Units'
                                Expression = { 
                                    try {
                                        $_.Qualifiers.Item("Units").Value
                                    } catch {
                                        ""
                                    }
                                }
                            }


                    }
                }, @{
                    Name='Methods'
                    Expression={
                        $_.Methods | 
                            Select-Object @{
                                Label='MethodName'
                                Expression={$_.Name}
                            }, @{
                                Label='Description'
                                Expression={$_.Qualifiers.Item("Description").Value}
                            }, @{
                                Label='MapsTo'
                                Expression = { 
                                    try {
                                        $_.Qualifiers.Item("MappingStrings").Value
                                        
                                    } catch {
                                        ""
                                    }
                                }
                            }, @{
                                Label='Privileges'
                                Expression = { 
                                    try {
                                        $_.Qualifiers.Item("Privileges").Value -join ','                                        
                                    } catch {
                                        ""
                                    }
                                }
                            }, @{
                                Label='IsStatic'
                                Expression = {
                                    try {
                                        [bool]$_.Qualifiers.Item("Static").Value
                                    } catch {
                                        $false 
                                    }
                                }
                            }, @{
                                Label='IsConstructor'
                                Expression = {
                                    try {
                                        [bool]$_.Qualifiers.Item("Constructor").Value
                                    } catch {
                                        $false 
                                    }
                                }
                            }, @{
                                Label='IsDestructor'
                                Expression = {
                                    try {
                                        [bool]$_.Qualifiers.Item("Destructor").Value
                                    } catch {
                                        $false 
                                    }
                                }
                            }            
                    }
                }, @{
                    Name='IsEvent'
                    Expression = { $_.Derivation -contains '__Event' } 
                }, @{
                    Name='IsSystemClass'
                    Expression = { $_.Derivation -contains '__SystemClass' } 
                }, @{
                    Name='IsDeprecated'
                    Expression = { 
                        try {
                            $_.Qualifiers.Item("DEPRECATED").Value
                        } catch {
                            $false
                        }
                    } 
                }|
                Where-Object {
                    if ($OnlyIfItCanBeCreated) {
                        $stillOk = $_.CanCreate
                        if (-not $stillOk) { return } 
                    }
                    if ($OnlyIfItCanBeDeleted) {
                        $stillOk = $_.CanDelete
                        if (-not $stillOk) { return } 
                    }
                    if ($OnlyIfItCanBeUpdated) {
                        $stillOk = $_.CanUpdate
                        if (-not $stillOk) { return }
                    }
                    if ($ForAPropertyLike) {
                        $stillOk = $_.Properties | 
                                Where-Object { 
                                    ($_.PropertyName -like $ForAPropertyLike -or
                                    $_.Description -like $ForAPropertyLike) -and 
                                    (((-not $OnlyIfThePropertyCanWrite) -or 
                                    ($OnlyIfThePropertyCanWrite -and $_.CanWrite)))
                                }
                        if (-not $stillOk ) { return }
                    }
                    
                    if ($ForAMethodLike) {
                        $stillOk = $_.Methods | 
                            Where-Object  {
                                $_.MethodName -like $ForAMethodLike -or
                                $_.Description -like $ForAMethodLike
                            }
                        if (-not $stillOk) { return }
                    }
                    
                    if ($OnlySearchForAnEvent) {
                        $stillOk = $_.IsEvent
                        if (-not $stillOk) { return }
                    }
                    
                    if ($ForTextInDescription) {
                        $stillOk = $_.Description -like "*$ForTextInDescription*"
                        if (-not $stillOk) { return } 
                    }
         
                    
                                    
                    return $true                
                
                } | ForEach-Object {
                    $_.psobject.typenames.clear()
                    $_.psobject.typenames.add('WmiInfo')
                    $_
                }
        
        Write-Progress "Getting WMI Data" "Complete" -Completed
    }    
}