forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind-cimclass.ps1
More file actions
105 lines (90 loc) · 3.73 KB
/
find-cimclass.ps1
File metadata and controls
105 lines (90 loc) · 3.73 KB
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
#search WMI for a class
Function Find-CimClass {
[CmdletBinding()]
[alias('fcc')]
[OutputType([Microsoft.Management.Infrastructure.CimClass])]
Param(
[Parameter(Position = 0, Mandatory, HelpMessage = "Enter the name of a CIM/WMI class. Wildcards are permitted.")]
[ValidateNotNullOrEmpty()]
[string]$Classname,
[Parameter(HelpMessage = "Enter a pattern for class names to EXCLUDE from the results. You can use wildcards or regular expressions.")]
[string]$Exclude,
[Parameter(HelpMessage = "Enter the name of a computer to search.")]
[ValidateNotNullOrEmpty()]
[string]$Computername = $env:COMPUTERNAME
)
Write-Verbose "Starting $($myinvocation.MyCommand)"
#the command requires CIM cmdlets which won't work on non-Windows platforms.
if ((Test-IsPSWindows)) {
#define a hashtable of parameters to splat to Write-Progress
$progParams = @{
Activity = $myinvocation.MyCommand
PercentComplete = 0
Status = "Enumerating namespaces on $($Computername.ToUpper())"
CurrentOperation = "Creating temporary CIMSession"
}
Write-Progress @progParams
Write-Verbose "Creating a temporary CIMSession to $($Computername.ToUpper())."
Try {
$cs = New-CimSession -ComputerName $computername -ErrorAction Stop
}
Catch {
Write-Warning "Failed to connect to $($Computername.ToUpper())"
Write-Warning $_.exception.Message
#bail out of the function
return
}
$progParams.CurrentOperation = "Building namespace list"
#build a list of namespaces
Function _enumnamespace {
[cmdletbinding()]
Param(
[string]$Namespace = "Root",
[cimsession]$session
)
Get-CimInstance -Namespace $namespace -ClassName __Namespace -cimsession $session |
ForEach-Object {
$n = Join-Path $Namespace $_.Name
#write the namespace path to the pipeline
$n
#recurse through each namespace
_enumnamespace -Namespace $n -session $session
}
}
#build a list of namespaces
$namespaces = _enumnamespace -session $CS | Sort-Object
#enumerate namespaces and search for the class
Write-Verbose "Searching for class $class"
if ($Exclude) {
Write-Verbose "Using an exclude pattern of $Exclude"
}
$i = 0
foreach ($ns in $namespaces) {
$i++
$pct = ($i / $namespaces.count) * 100
$progParams.PercentComplete = $pct
$progParams.Status = "Searching for class $classname in $($namespaces.count) namespaces"
$progParams.CurrentOperation = "processing \\$($computername.toUpper())\$ns"
Write-Progress @progparams
Write-Verbose $ns
Try {
$classes = Get-CimClass -Namespace $ns -ClassName $classname -CimSession $cs -ErrorAction Stop
if ($classes -AND $Exclude) {
$classes.Where( { $_.cimclassname -notmatch $Exclude }) | Sort-Object -Property CimClassName
}
else {
$classes | Sort-Object -Property CimClassName
}
}
Catch {
#ignore error if class not found
}
} #foreach ns
Write-Verbose "Removing tempory CIMSession"
$cs | Remove-CimSession
}
else {
Write-Warning "This command requires a Windows platform."
}
Write-Verbose "Ending $($myinvocation.MyCommand)"
} #close function