forked from dindenver/Backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WMIAdmin.psm1
489 lines (476 loc) · 15.4 KB
/
WMIAdmin.psm1
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# ******************************************************************************
#
# Script Name: WMIAdmin.psm1
# Version: 1.0
# Author: Dave M
# Date Created: 2/13/2012
# _______________________________________
#
# MODIFICATIONS:
# Date Modified:
# Modified By:
# Reason for modification:
# What was modified:
#
# Description: Provides WMI cmdlets to powershell.
#
# Usage:
# Import-Module WMIAdmin
#
# ******************************************************************************
Function Get-WMIClassesWithQualifiers
{
<#
.Synopsis
This function finds wmi classes with a specific qualifier
.Description
This function allows you to explore WMI namespaces to find classes with a
specific qualifier. Search for qualifiers such as dynamic, abstract,
supportsupdate, singleton
.Example
Get-WmiClassesWithQualifiers -q dynamic
finds all wmi classes in default namespace (root/cimv2) that are dynamic
.Example
Get-WmiClassesWithQualifiers -q supportsupdate -n root/wmi
Finds all wmi classes in root/wmi that support updating
.Parameter Qualifier
The qualifier to search for. Ex: dynamic, abstract, supportsupdate,
Singleton
.Parameter Namespace
The namespace to search. Default is Root/Cimv2
.Role
Meta
.Component
HSGWMIModuleV6
.Notes
NAME: Get-WmiClassesWithQualifiers
AUTHOR: ed wilson, msft
LASTEDIT: 10/16/2011 13:49:42
KEYWORDS: Scripting Techniques, WMI
HSG: HSG-10-22-11, 10-24-11
.Link
Http://www.ScriptingGuys.com
#Requires -Version 2.0
#>
Param([string]$qualifier = "dynamic",
[string]$namespace = "root\cimv2")
$classes = Get-WmiObject -list -namespace $namespace
foreach($class in $classes)
{
$query = "Select * from meta_class where __this isa ""$($class.name)"" "
$a = Get-WmiObject -Query $query -Namespace $namespace |
Select-Object -Property __class, qualifiers
if($a.qualifiers | ForEach-Object { $_ | Where-Object { $_.name -match "$qualifier" }})
{ $a.__class }
} #end foreach $class
} #end function Get-WMIClassesWithQualifiers
Function Out-TempFile
{
<#
.Role
Helper
.Component
HSGWMIModuleV6
#>
begin { $tmpfile= [io.path]::GetTempFileName() }
Process { $_ >> $tmpFile }
End { notepad $tmpfile | out-null
Remove-Item $tmpFile }
}
function New-Underline
{
<#
.Synopsis
Creates an underline the length of the input string
.Example
New-Underline -strIN "Hello world"
.Example
New-Underline -strIn "Morgen welt" -char "-" -sColor "blue" -uColor "yellow"
.Example
"this is a string" | New-Underline
.Role
Helper
.Component
HSGWMIModuleV6
.Notes
NAME:
AUTHOR: Ed Wilson
LASTEDIT: 5/20/2009
KEYWORDS:
.Link
Http://www.ScriptingGuys.com
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true,Position = 0,valueFromPipeline=$true)]
[string]
$strIN,
[string]
$char = "=",
[string]
$sColor = "Green",
[string]
$uColor = "darkGreen",
[switch]
$pipe
) #end param
$strLine= $char * $strIn.length
if(-not $pipe)
{
Write-Host -ForegroundColor $sColor $strIN
Write-Host -ForegroundColor $uColor $strLine
}
Else
{
$strIn
$strLine
}
} #end new-underline function
Function Get-WmiClassMethods
{
<#
.Synopsis
This function returns implemented methods for a WMI class
.Description
This function returns implemented methods for a WMI class
.Example
Get-WmiClassMethods Win32_logicaldisk
Returns implemented methods from the Win32_logicaldisk class
.Example
Get-WmiClassMethods -class bcdstore -namespace root\wmi
Returns methods of the bcdStore WMI class from the root\wmi namespace
.EXAMPLE
Get-WmiClassMethods -class Win32_networkadapter -computer DC1
Returns methods from the Win32_networkadapter wmi class in the root\cimv2
namespace from a remote server named DC1
.Parameter Class
The name of the WMI class
.Parameter Namespace
The name of the WMI namespace. Defaults to root\cimv2
.Parameter Computer
The name of the computer. Defaults to local computer
.Role
Meta
.Component
HSGWMIModuleV6
.Notes
NAME: Get-WmiClassMethods
AUTHOR: ed wilson, msft
LASTEDIT: 10/17/2011 13:43:24
KEYWORDS:
HSG: HSG-10-24-11, based upon WES-3-12-11
.Link
Http://www.ScriptingGuys.com
#Requires -Version 2.0
#>
Param(
[Parameter(Mandatory = $true,Position = 0)]
[string]$class,
[string]$namespace = "root\cimv2",
[string]$computer = $env:computername
)
$abstract = $false
$method = $null
[wmiclass]$class = "\\{0}\{1}:{2}" -f $computer,$namespace,$class
Foreach($q in $class.Qualifiers)
{ if ($q.name -eq 'Abstract') {$abstract = $true} }
If(!$abstract)
{
Foreach($m in $class.methods)
{
Foreach($q in $m.qualifiers)
{
if($q.name -match "implemented")
{
$method += $m.name + "`r`n"
} #end if name
} #end foreach q
} #end foreach m
if($method)
{
New-Underline -strIN $class.name
New-Underline "METHODS" -char "-"
}
$method
} #end if not abstract
$abstract = $false
$method = $null
# } #end foreach class
} #end function Get-WmiClassMethods
Function Get-WmiClassProperties
{
<#
.Synopsis
This function returns writable properties for a WMI class
.Description
This function returns writable properties for a WMI class
.Example
Get-WMIClassProperties Win32_logicaldisk
Returns writable properties from the Win32_logicaldisk class
.Example
Get-WMIClassProperties -class bcdstore -namespace root\wmi
Returns properties of the bcdStore WMI class from the root\wmi namespace
.EXAMPLE
Get-WMIClassProperties -class Win32_networkadapter -computer DC1
Returns properties from the Win32_networkadapter wmi class in the root\cimv2
namespace from a remote server named DC1
.Parameter Class
The name of the WMI class
.Parameter Namespace
The name of the WMI namespace. Defaults to root\cimv2
.Parameter Computer
The name of the computer. Defaults to local computer
.Role
Meta
.Component
HSGWMIModuleV6
.Notes
NAME: Get-WMIClassProperties
AUTHOR: ed wilson, msft
LASTEDIT: 10/17/2011 13:43:24
KEYWORDS:
HSG: HSG-10-24-11, based upon WES-3-12-11
.Link
Http://www.ScriptingGuys.com
#Requires -Version 2.0
#>
Param(
[Parameter(Mandatory = $true,Position = 0)]
[string]$class,
[string]$namespace = "root\cimv2",
[string]$computer = $env:computername
)
$abstract = $false
$property = $null
[wmiclass]$class = "\\{0}\{1}:{2}" -f $computer,$namespace,$class
Foreach($q in $class.Qualifiers)
{ if ($q.name -eq 'Abstract') {$abstract = $true} }
If(!$abstract)
{
Foreach($p in $class.Properties)
{
Foreach($q in $p.qualifiers)
{
if($q.name -match "write")
{
$property += $p.name + "`r`n"
} #end if name
} #end foreach q
} #end foreach p
if($property)
{
New-Underline -strIN $class.name
New-Underline "PROPERTIES" -char "-"
}
$property
} #end if not abstract
$abstract = $false
$property = $null
# } #end foreach class
} #end function Get-WmiClassProperties
function Get-WmiKey
{
<#
.Synopsis
This function returns the key property of a WMI class
.Description
This function returns the key property of a WMI class
.Example
Get-WMIKey win32_bios
Returns the key properties for the Win32_bios WMI class in root\ciimv2
.Example
Get-WmiKey -class Win32_product
Returns the key properties for the Win32_Product WMI class in root\cimv2
.Example
Get-WmiKey -class systemrestore -namespace root\default
Gets the key property from the systemrestore WMI class in the root\default
WMI namespace.
.Parameter Class
The name of the WMI class
.Parameter Namespace
The name of the WMI namespace. Defaults to root\cimv2
.Parameter Computer
The name of the computer. Defaults to local computer
.Role
Meta
.Component
HSGWMIModuleV6
.Notes
NAME: Get-WMIKey
AUTHOR: ed wilson, msft
LASTEDIT: 10/18/2011 17:38:20
KEYWORDS: Scripting Techniques, WMI
HSG: HSG-10-24-2011
.Link
Http://www.ScriptingGuys.com
#Requires -Version 2.0
#>
Param(
[Parameter(Mandatory = $true,Position = 0)]
[string]$class,
[string]$namespace = "root\cimv2",
[string]$computer = $env:computername
)
[wmiclass]$class = "\\{0}\{1}:{2}" -f $computer,$namespace,$class
$class.Properties |
Select-object @{Name="PropertyName";Expression={$_.name}} -ExpandProperty Qualifiers | Where-object {$_.Name -eq "key"} | ForEach-Object {$_.PropertyName}
} #end GetWmiKey
Function Get-WmiKeyvalue
{
<#
.Synopsis
This function gets the __Path values for a WMI class
.Description
This function gets the __path values which will show value of key property
.Example
Get-WmiKeyvalue win32_bios
Gets the path to the Win32_bios class
.Example
Get-WmiKeyvalue -class win32_process
Gets the paths to each process running on computer
.Example
Get-WmiKeyvalue -class SystemRestore -namespace root\default
Gets paths of SystemRestore (requires admin rights)
.Parameter Class
The wmi class name
.Parameter Computername
The name of the computer
.Parameter Namespace
The namespace containing the WMI class
.Role
Meta
.Component
HSGWMIModuleV6
.Notes
NAME: Get-WmiKeyvalue
AUTHOR: ed wilson, msft
LASTEDIT: 10/19/2011 17:52:10
KEYWORDS: Scripting Techniques, WMI
HSG: HSG-10-26-11
.Link
Http://www.ScriptingGuys.com
#Requires -Version 2.0
#>
Param(
[Parameter(Mandatory=$true)]
[string]$class,
[string]$computername = $env:COMPUTERNAME,
[string]$namespace = "root\cimv2"
)
Get-WmiObject -Class $class -ComputerName $computername -Namespace $namespace |
Select __PATH
} #end function get-WmiKeyvalue
Filter HasWmiValue
{
<#
.Synopsis
This is a filter that will remove empty property values
from the returned WMI information
.Description
This removes empty property values from returned WMI information.
It is useful because most WMI classes return many lines of empty
properties. By using this filter, the returned WMI information is
easier to read. It also makes it easier to find required information.
.Example
Get-WmiObject -class win32_bios | HasWMiValue
Returns BIOS information from the local computer. Only WMI properties
that contain a value are returned.
.Example
gwmi -cl win32_bios -cn remotehost | HasWMIValue
Returns BIOS information from a remote computer named remotehost. Only
WMI properties that contain a value are returned.
.Role
Helper
.Component
HSGWMIModuleV6
.Notes
NAME: HasWmiValue
AUTHOR: ed wilson, msft
LASTEDIT: 10/20/2011 12:33:45
KEYWORDS:
HSG: HSG-10-27-2011
.Link
Http://www.ScriptingGuys.com
#Requires -Version 2.0
#>
$_.properties |
foreach-object -BEGIN { $_.path | new-underline -pipe} -Process {
If($_.value -AND $_.name -notmatch "__")
{
@{ $($_.name) = $($_.value) }
} #end if
} #end foreach property
} #end filter HasWmiValue
Function Get-WmiClassesAndQuery
{
<#
.Synopsis
This function searches for WMI classes based upon a wild card
and then it will query those WMI classes.
.Description
This function searches for WMI classes based upon a wild card
pattern, and then it will query those WMI classes. It can use
standard wild card patterns. You can specify namespace, and
remote computer name. This version does not accept alternate
credentials, but you can use runas to run PowerShell with alternate
credentials. It automatically filters out abstract classes.
.Example
Get-WMIClassesAndQuery -class *disk*
Searches for all WMI classes that contain the letters disk in the class
name. It then filters out only the non-abstract classes, and then queries
them. The WMI classes queried come from the root\cimv2 namespace and are
on the local computer.
.Example
Get-WmiClassesAndQuery -class *adapter* -namespace "root\wmi" -cn dc1
Gets all WMI classes from the root\wmi namespace that are on the remote
computer named dc1. It then queries those non-abstract classes and returns
the information.
.Parameter Class
A WMI class name, or wild card pattern that will find WMI class names
.Parameter Namespace
A valid WMI namespace. By default it is root\cimv2
.Parameter Computer
The computer from which to return information. Defaults to local computer.
.Role
Query
.Component
HSGWMIModuleV6
.Notes
NAME: Get-WmiClassesAndQuery
AUTHOR: ed wilson, msft
LASTEDIT: 10/27/2011 13:52:46
KEYWORDS: Scripting Techniques, WMI
HSG: HSG-10-28-11
.Link
Http://www.ScriptingGuys.com
#Requires -Version 2.0
#>
Param(
[Parameter(Mandatory=$true,Position=0,valueFromPipeline=$true)]
[string]$class,
[string]$namespace = "Root\cimv2",
[string]$computer = $env:computername
)
Get-WmiObject -List $class -Namespace $namespace -ComputerName $computer |
ForEach-Object {
$abstract = $false
[wmiclass]$class = "\\{0}\{1}:{2}" -f $computer,$namespace,$_.name
Foreach($q in $_.Qualifiers)
{ if ($q.name -eq 'Abstract') {$abstract = $true} }
If(!$abstract)
{
Get-WmiObject -Class $_.name -Namespace $namespace -ComputerName $computer
} #end if !$abstract
} #end Foreach-object
} #end function get-wmiclassesandquery
# *** Aliases and descriptions ***
New-Alias -Name gwq -Value Get-WmiClassesAndQuery -Description "HSG WMI module: query" -Scope "global"
New-Alias -Name gwcq -Value Get-WMIClassesWithQualifiers -Description "HSG WMI module: meta" -Scope "global"
New-Alias -Name gwcm -Value Get-WmiClassMethods -Description "HSG WMI module: meta" -Scope "global"
New-Alias -Name gwcp -Value Get-WmiClassProperties -Description "HSG WMI module: meta" -Scope "global"
New-Alias -Name gwck -Value Get-WmiKey -Description "HSG WMI module: meta" -Scope "global"
New-Alias -Name gwkv -Value Get-WmiKeyvalue -Description "HSG WMI module: meta" -Scope "global"
New-Alias -Name hwv -Value haswmivalue -Description "HSG WMI module: helper" -Scope "global"
New-Alias -Name nu -Value New-Underline -Description "HSG WMI module: helper" -Scope "global"
New-Alias -Name otf -Value Out-TempFile -Description "HSG WMI module: helper" -Scope "global"
Export-ModuleMember -Function * -Alias *