-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWMI.cs
213 lines (184 loc) · 6.98 KB
/
WMI.cs
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
using System;
using System.Collections.Generic;
using System.Management;
using System.ServiceProcess;
using static System.Management.ManagementObjectCollection;
namespace ZenStates.Core
{
public static class WMI
{
public static object TryGetProperty(ManagementObject wmiObj, string propertyName)
{
object retval = null;
try
{
retval = wmiObj.GetPropertyValue(propertyName);
}
catch (ManagementException ex)
{
Console.WriteLine(ex.Message);
}
return retval;
}
//root\wmi
public static ManagementScope Connect(string scope)
{
try
{
var sc = new ServiceController("Winmgmt");
if (sc.Status != ServiceControllerStatus.Running)
throw new ManagementException(@"Windows Management Instrumentation service is not running");
ManagementScope mScope = new ManagementScope($@"{scope}");
mScope.Connect();
if (mScope.IsConnected)
return mScope;
else
throw new ManagementException($@"Failed to connect to {scope}");
}
catch (ManagementException ex)
{
Console.WriteLine(@"WMI: {0}", ex.Message);
throw;
}
}
public static ManagementObject Query(string scope, string wmiClass)
{
try
{
using (var searcher = new ManagementObjectSearcher($"{scope}", $"SELECT * FROM {wmiClass}"))
{
ManagementObjectEnumerator enumerator = searcher.Get().GetEnumerator();
if (enumerator.MoveNext())
return enumerator.Current as ManagementObject;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
public static List<string> GetWmiNamespaces(string root)
{
List<string> namespaces = new List<string>();
try
{
ManagementClass nsClass =
new ManagementClass(new ManagementScope(root), new ManagementPath("__namespace"), null);
foreach (var obj in nsClass.GetInstances())
{
var ns = (ManagementObject)obj;
string namespaceName = root + "\\" + ns["Name"];
namespaces.Add(namespaceName);
namespaces.AddRange(GetWmiNamespaces(namespaceName));
}
namespaces.Sort(StringComparer.OrdinalIgnoreCase);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return namespaces;
}
public static List<string> GetClassNamesWithinWmiNamespace(string wmiNamespaceName)
{
List<string> classNames = new List<string>();
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher
(new ManagementScope(wmiNamespaceName),
new WqlObjectQuery("SELECT * FROM meta_class"));
ManagementObjectCollection objectCollection = searcher.Get();
foreach (var obj in objectCollection)
{
var wmiClass = (ManagementClass)obj;
string stringified = wmiClass.ToString();
string[] parts = stringified.Split(':');
classNames.Add(parts[1]);
}
classNames.Sort(StringComparer.OrdinalIgnoreCase);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return classNames;
}
public static string GetInstanceName(string scope, string wmiClass)
{
using (ManagementObject queryObject = Query(scope, wmiClass))
{
string name = "";
if (queryObject == null)
return name;
try
{
var obj = TryGetProperty(queryObject, "InstanceName");
if (obj != null) name = obj.ToString();
}
catch
{
// ignored
}
return name;
}
}
public static ManagementBaseObject InvokeMethod(ManagementObject mo, string methodName, string inParamName, uint arg)
{
try
{
// Obtain in-parameters for the method
ManagementBaseObject inParams = mo.GetMethodParameters($"{methodName}");
// Add the input parameters.
if (inParams != null)
inParams[$"{inParamName}"] = arg;
// Execute the method and obtain the return values.
ManagementBaseObject outParams = mo.InvokeMethod($"{methodName}", inParams, null);
return outParams;
}
catch (ManagementException)
{
return null;
}
}
public static ManagementBaseObject InvokeMethodAndGetValue(ManagementObject mo, string methodName, string propName,
string inParamName, uint arg)
{
try
{
ManagementBaseObject outParams = InvokeMethod(mo, methodName, inParamName, arg);
return (ManagementBaseObject)outParams?.Properties[$"{propName}"].Value;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
public static byte[] RunCommand(ManagementObject mo, uint commandID, uint commandArg = 0x0)
{
try
{
// Obtain in-parameters for the method
ManagementBaseObject inParams = mo.GetMethodParameters("RunCommand");
// Add the input parameters.
byte[] buffer = new byte[8];
var cmd = BitConverter.GetBytes(commandID);
var arg = BitConverter.GetBytes(commandArg);
Buffer.BlockCopy(cmd, 0, buffer, 0, 4);
Buffer.BlockCopy(arg, 0, buffer, 4, 4);
inParams["Inbuf"] = buffer;
// Execute the method and obtain the return values.
ManagementBaseObject outParams = mo.InvokeMethod("RunCommand", inParams, null);
// return outParam
ManagementBaseObject pack = (ManagementBaseObject)outParams?.Properties["Outbuf"].Value;
return (byte[])pack?.GetPropertyValue("Result");
}
catch (ManagementException ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
}
}