-
Notifications
You must be signed in to change notification settings - Fork 164
/
wmi_provider_association.ps1
93 lines (74 loc) · 3.24 KB
/
wmi_provider_association.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
<#
Author: Matthew Graeber (@mattifestation)
License: BSD 3-Clause
#>
function Get-WmiNamespace {
[OutputType([String])]
Param (
[String]
[ValidateNotNullOrEmpty()]
$Namespace = 'ROOT',
[Switch]
$Recurse
)
$BoundParamsCopy = $PSBoundParameters
$null = $BoundParamsCopy.Remove('Namespace')
# Exclude locale specific namespaces
Get-WmiObject -Class __NAMESPACE -Namespace $Namespace -Filter 'NOT Name LIKE "ms_4%"' | ForEach-Object {
$FullyQualifiedNamespace = '{0}\{1}' -f $_.__NAMESPACE, $_.Name
$FullyQualifiedNamespace
if ($Recurse) {
Get-WmiNamespace -Namespace $FullyQualifiedNamespace @BoundParamsCopy
}
}
}
function Get-WmiProviderAssociation {
$UnhostedClasses = New-Object 'Collections.ObjectModel.Collection`1[System.Management.ManagementClass]'
Get-WmiNamespace -Recurse | ForEach-Object {
$Namespace = $_
$ClassProviderMapping = @{}
Get-WmiObject -Namespace $Namespace -List | % {
if ($_.Qualifiers['Provider']) {
$HostingProvider = $_.Qualifiers['Provider'].Value.ToLower()
if (-not $ClassProviderMapping.ContainsKey($HostingProvider)) {
$ClassProviderMapping[$HostingProvider] = New-Object 'Collections.ObjectModel.Collection`1[System.Management.ManagementClass]'
}
$ClassProviderMapping[$HostingProvider].Add($_)
} else {
$UnhostedClasses.Add($_)
}
}
Get-WmiObject -Namespace $_ -Class __Win32Provider | ForEach-Object {
$ProviderCLSID = $_.CLSID
$ClientCLSID = $_.ClientLoadableCLSID
$ProviderImage = (Invoke-WmiMethod -Namespace root/default -Class StdRegProv -Name GetStringValue -ArgumentList @([UInt32] 2147483648, "CLSID\$ProviderCLSID\InprocServer32", $null)).sValue
$ClientImage = (Invoke-WmiMethod -Namespace root/default -Class StdRegProv -Name GetStringValue -ArgumentList @([UInt32] 2147483648, "CLSID\$ClientCLSID\InprocServer32", $null)).sValue
$HostedClasses = $null
if ($ClassProviderMapping.ContainsKey($_.Name.ToLower())) {
$HostedClasses = $ClassProviderMapping[$_.Name.ToLower()]
}
$Properties = [Ordered] @{
Namespace = $Namespace
ProviderName = $_.Name
HostingModel = $_.HostingModel
ProviderImage = $ProviderImage
ClientImage = $ClientImage
HostedClasses = $HostedClasses
}
New-Object -TypeName PSObject -Property $Properties
}
}
# Add a catch-all entry for all classes for which there is no provider
if ($UnhostedClasses.Count -gt 0) {
# Create a "null" provider object
$Properties = [Ordered] @{
Namespace = $null
ProviderName = $null
HostingModel = $null
ProviderImage = $null
ClientImage = $null
HostedClasses = $UnhostedClasses
}
New-Object -TypeName PSObject -Property $Properties
}
}