-
Notifications
You must be signed in to change notification settings - Fork 17
/
enum-wmi.ps1
148 lines (119 loc) · 4.55 KB
/
enum-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
<#
.SYNOPSIS
script to enumerate WMI at a given node matching a given class
.DESCRIPTION
.NOTES
File Name : enum-wmi.ps1
Author : jagilber
Version : 160612
History : 160414 original
.EXAMPLE
.\enum-wmi.ps1 -namespace root\cimv2 -class Win32_TS
.PARAMETER namespace
provide wmi namespace where to start enumeration. example root\cimv2
.PARAMETER class
provide wmi class or partial class to enumerate. ex Win32_TS
#>
Param(
[parameter(Mandatory=$false,HelpMessage="Enter the namespace. ex: root\cimv2")]
[string] $nameSpace = "root\cimv2\TerminalServices",
[parameter(Mandatory=$false,HelpMessage="Enter string class filter. ex: Win32_Drive")]
[string] $classFilter = ""
)
$ErrorActionPreference = "silentlycontinue"
$logFile = "wmi-enumLog.txt"
cls
#-----------------------------------------------------------------------------------------------
function main()
{
Stop-Transcript
$error.Clear()
Start-Transcript -Path $logfile
log-info "*******************************************"
log-info "*******************************************"
log-info "starting"
log-info "*******************************************"
log-info "*******************************************"
$wmiNamespaces = enumerate-namespaces -wminamespace $nameSpace
foreach($wmiNamespace in $wmiNamespaces)
{
if($wmiNamespace.Contains("ms_409"))
{
continue
}
"*******************************************"
"Namespace:$($wmiNamespace)"
"*******************************************"
$wmiClasses = Get-CimClass -ClassName * -Namespace $wmiNamespace
foreach ($wmiClass in $wmiClasses)
{
if($wmiClass.CimClassName.Contains("ms_409"))
{
continue
}
if(![string]::IsNullOrEmpty($classFilter) -and !$wmiClass.CimClassName.ToLower().Contains($classFilter))
{
continue
}
if($wmiClass.CimClassMethods.Count -gt 0)
{
"*******************************************"
"Class:$($wmiNamespace)\$($wmiClass.CimClassName) Methods"
"*******************************************"
foreach($method in $wmiClass.CimClassMethods)
{
$method | fl *
#log-info $method
}
"*******************************************"
}
if($wmiClass.CimClassName.StartsWith("__") -or $wmiClass.CimClassName.StartsWith("CIM"))
{
continue
}
else
{
"*******************************************"
"Class:$($wmiNamespace)\$($wmiClass.CimClassName)"
"*******************************************"
$wmiObj = Get-WmiObject -Namespace $wmiNamespace -Class $wmiClass.CimClassName -Recurse #-ErrorAction SilentlyContinue
if($wmiObj -ne $null)
{
#log-info "Value:`t`t$($wmiObj)"
#"`t`t$($wmiObj)"
$wmiObj#.Properties | fl *
"*******************************************"
}
#log-info
#write-host $wmiObj
}
}
}
"*******************************************"
log-info "*******************************************"
log-info "finished"
log-info "*******************************************"
log-info "*******************************************"
stop-Transcript
}
#-----------------------------------------------------------------------------------------------
function enumerate-namespaces($wmiNamespace)
{
$wmiRootNamespaces = new-object Collections.ArrayList
[void]$wmiRootNamespaces.Add($wmiNamespace)
foreach($name in (Get-WmiObject -Namespace $wmiNamespace -Class __NAMESPACE).Name)
{
$tempName = "$($wmiNamespace)\$($name)"
[void]$wmiRootNamespaces.AddRange(@(enumerate-namespaces -wminamespace $tempName ))
}
return $wmiRootNamespaces
}
#-----------------------------------------------------------------------------------------------
function log-info($data)
{
$data = "$([System.DateTime]::Now):$($data)`n"
$data
#out-file -Append -InputObject $data -FilePath $logFile
}
#-----------------------------------------------------------------------------------------------
main