-
Notifications
You must be signed in to change notification settings - Fork 13
/
WmiService.cs
540 lines (451 loc) · 15.1 KB
/
WmiService.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
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Threading;
using System.Data;
// From: http://blog.jrboelens.com/?p=8
namespace MemCacheDManager
{
#region --- Enums ---
//Reference: http://msdn2.microsoft.com/en-us/library/aa389390(VS.85).aspx
public enum ServiceType
{
KernalDriver = 1,
FileSystemDriver = 2,
Adapter = 4,
RecognizerDriver = 8,
OwnProcess = 16,
ShareProcess = 32,
InteractiveProcess = 256,
}
public enum ErrorControl
{
/// <summary>
/// Ignore. User is not notified.
/// </summary>
Ignore = 0,
/// <summary>
/// Normal. User is notified.
/// </summary>
Normal = 1,
/// <summary>
/// Severe. System is restarted with the last-known-good configuration.
/// </summary>
Severe = 2,
/// <summary>
/// Critical. System attempts to restart with a good configuration.
/// </summary>
Critical = 3
}
public enum StartMode
{
Boot,
System,
Auto,
Manual,
Disabled,
}
public enum ReturnValue
{
Success = 0,
NotSupported = 1,
AccessDenied = 2,
DependentServicesRunning = 3,
InvalidServiceControl = 4,
ServiceCannotAcceptControl = 5,
ServiceNotActive = 6,
ServiceRequestTimeout = 7,
UnknownFailure = 8,
PathNotFound = 9,
ServiceAlreadyRunning = 10,
ServiceDatabaseLocked = 11,
ServiceDependencyDeleted = 12,
ServiceDependencyFailure = 13,
ServiceDisabled = 14,
ServiceLogonFailure = 15,
ServiceMarkedForDeletion = 16,
ServiceNoThread = 17,
StatusCircularDependency = 18,
StatusDuplicateName = 19,
StatusInvalidName = 20,
StatusInvalidParameter = 21,
StatusInvalidServiceAccount = 22,
StatusServiceExists = 23,
ServiceAlreadyPaused = 24,
}
public enum State
{
Stopped,
StartPending,
StopPending,
Running,
ContinuePending,
PausePending,
Paused,
Unknown
}
#endregion
public class WmiService
{
public string _displayName;
[Business.DataTableConverter.Conversion(AllowDbNull = false, DataTableConversion = true, KeyField = false)]
public string DisplayName
{
get { return _displayName; }
set { _displayName = value; }
}
public string _pathName;
[Business.DataTableConverter.Conversion(AllowDbNull = false, DataTableConversion = true, KeyField = false)]
public string PathName
{
get { return _pathName; }
set { _pathName = value; }
}
public ServiceType ServiceType;
public ErrorControl ErrorControl;
public StartMode StartMode;
public bool DesktopInteract;
public string StartName;
public string StartPassword;
public string LoadOrderGroup;
public string LoadOrderGroupDependencies;
public string ServiceDependencies;
public bool AcceptPause;
public bool AcceptStop;
public int CheckPoint;
public string CreationClassName;
public string Description;
public int ExitCode;
public DateTime InstallDate;
public string _name;
[Business.DataTableConverter.Conversion(AllowDbNull = false, DataTableConversion = true, KeyField = false)]
public string Name
{
get { return _name; }
set { _name = value; }
}
public int ProcessId;
public int ServiceSpecificExitCode;
public bool Started;
public State State;
public string Status;
public string SystemCreationClassName;
public string SystemName;
public int TagId;
public int WaitHint;
public ManagementObject WmiServiceObject;
#region --- Fields ---
private const string CLASSNAME = "Win32_Service";
#endregion
#region --- Methods ---
#region Create
public static ReturnValue Create(string machineName, string serviceName, string displayName, string serviceLocation, StartMode startMode, string username, string password)
{
return Create(machineName, null, null, serviceName, displayName, serviceLocation, startMode, username, password);
}
public static ReturnValue Create(string machineName, string impersonationUsername, string impersonationPassword, string serviceName, string displayName, string serviceLocation, StartMode startMode, string username, string password)
{
try
{
string methodName = "Create";
object[] parameters = new object[]
{
serviceName, // Name
displayName, // Description
serviceLocation, // Location
Convert.ToInt32(ServiceType.OwnProcess), // ServiceType
Convert.ToInt32(ErrorControl.Normal), // Error Control
GetStartModeString(startMode), // Start Mode
false, // Desktop Interaction
username, // Username
password, // Password
null, // Service Order Group
null // Load Order Dependencies
};
return (ReturnValue)WmiHelper.InvokeStaticMethod(machineName, impersonationUsername, impersonationPassword, CLASSNAME, methodName, parameters);
}
catch
{
return ReturnValue.UnknownFailure;
}
}
#endregion
#region Delete
public static ReturnValue Delete(string machineName, string serviceName)
{
return Delete(machineName, null, null, serviceName);
}
public static ReturnValue Delete(string machineName, string impersonationUsername, string impersonationPassword, string serviceName)
{
try
{
string methodName = "Delete";
return (ReturnValue)WmiHelper.InvokeInstanceMethod(machineName, impersonationUsername, impersonationPassword, CLASSNAME, serviceName, methodName, null);
}
catch
{
return ReturnValue.UnknownFailure;
}
}
#endregion
#region Start
public static ReturnValue Start(string machineName, string serviceName)
{
return Start(machineName, null, null, serviceName);
}
public static ReturnValue Start(string machineName, string impersonationUsername, string impersonationPassword, string serviceName)
{
try
{
string methodName = "StartService";
return (ReturnValue)WmiHelper.InvokeInstanceMethod(machineName, impersonationUsername, impersonationPassword, CLASSNAME, serviceName, methodName, null);
}
catch
{
return ReturnValue.UnknownFailure;
}
}
#endregion
#region Stop
public static ReturnValue Stop(string machineName, string serviceName)
{
return Stop(machineName, null, null, serviceName);
}
public static ReturnValue Stop(string machineName, string impersonationUsername, string impersonationPassword, string serviceName)
{
try
{
string methodName = "StopService";
return (ReturnValue)WmiHelper.InvokeInstanceMethod(machineName, impersonationUsername, impersonationPassword, CLASSNAME, serviceName, methodName, null);
}
catch
{
return ReturnValue.UnknownFailure;
}
}
#endregion
#region Pause
public static ReturnValue Pause(string machineName, string serviceName)
{
return Pause(machineName, null, null, serviceName);
}
public static ReturnValue Pause(string machineName, string impersonationUsername, string impersonationPassword, string serviceName)
{
try
{
string methodName = "PauseService";
return (ReturnValue)WmiHelper.InvokeInstanceMethod(machineName, impersonationUsername, impersonationPassword, CLASSNAME, serviceName, methodName, null);
}
catch
{
return ReturnValue.UnknownFailure;
}
}
#endregion
#region Resume
public static ReturnValue Resume(string machineName, string serviceName)
{
return Resume(machineName, null, null, serviceName);
}
public static ReturnValue Resume(string machineName, string impersonationUsername, string impersonationPassword, string serviceName)
{
try
{
string methodName = "ResumeService";
return (ReturnValue)WmiHelper.InvokeInstanceMethod(machineName, impersonationUsername, impersonationPassword, CLASSNAME, serviceName, methodName, null);
}
catch
{
return ReturnValue.UnknownFailure;
}
}
#endregion
public static List<WmiService> GetAllServices(string machineName, string userName, string password)
{
return GetAllServices(machineName, userName, password, null);
}
public static List<WmiService> GetAllServices(string machineName, string userName, string password, string filterString)
{
List<WmiService> allServices = new List<WmiService>();
ManagementScope scope = WmiHelper.Connect(machineName, userName, password);
// Get all app pools.
SelectQuery selectQuery;
//if(filterString == null || filterString == String.Empty)
selectQuery = new SelectQuery(@"SELECT * FROM Win32_Service");
//else
// selectQuery = new SelectQuery(string.Format(@"SELECT * FROM Win32_Service WHERE {0}", filterString));
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, selectQuery);
foreach (ManagementObject managementObject in searcher.Get())
{
WmiService newService = new WmiService();
newService.WmiServiceObject = managementObject;
foreach(PropertyData propertyData in managementObject.Properties)
{
if (propertyData.Value == null)
continue;
switch(propertyData.Name)
{
case "DesktopInteract":
bool.TryParse(propertyData.Value.ToString(), out newService.DesktopInteract);
break;
case "DisplayName":
newService.DisplayName = propertyData.Value.ToString();
break;
case "ErrorControl":
newService.ErrorControl = (ErrorControl)Enum.Parse(typeof(ErrorControl), propertyData.Value.ToString(), true);
break;
case "LoadOrderGroup":
newService.LoadOrderGroup = propertyData.Value.ToString();
break;
case "LoadOrderGroupDependencies":
newService.LoadOrderGroupDependencies = propertyData.Value.ToString();
break;
case "PathName":
newService.PathName = propertyData.Value.ToString();
break;
case "ServiceDependencies":
newService.ServiceDependencies = propertyData.Value.ToString();
break;
case "ServiceType":
newService.ServiceType = (ServiceType)Enum.Parse(typeof(ServiceType), propertyData.Value.ToString().Replace(" ", ""), true);
break;
case "StartMode":
newService.StartMode = (StartMode)Enum.Parse(typeof(StartMode), propertyData.Value.ToString(), true);
break;
case "StartName":
newService.StartName = propertyData.Value.ToString();
break;
case "StartPassword":
newService.StartPassword = propertyData.Value.ToString();
break;
case "AcceptPause":
bool.TryParse(propertyData.Value.ToString(), out newService.AcceptPause);
break;
case "AcceptStop":
bool.TryParse(propertyData.Value.ToString(), out newService.AcceptStop);
break;
case "CheckPoint":
int.TryParse(propertyData.Value.ToString(), out newService.CheckPoint);
break;
case "CreationClassName":
newService.CreationClassName = propertyData.Value.ToString();
break;
case "Description":
newService.Description = propertyData.Value.ToString();
break;
case "ExitCode":
int.TryParse(propertyData.Value.ToString(), out newService.ExitCode);
break;
case "InstallDate":
DateTime.TryParse(propertyData.Value.ToString(), out newService.InstallDate);
break;
case "Name":
newService.Name = propertyData.Value.ToString();
break;
case "ProcessId":
int.TryParse(propertyData.Value.ToString(), out newService.ProcessId);
break;
case "ServiceSpecificExitCode":
int.TryParse(propertyData.Value.ToString(), out newService.ServiceSpecificExitCode);
break;
case "Started":
bool.TryParse(propertyData.Value.ToString(), out newService.Started);
break;
case "State":
newService.State = (State)Enum.Parse(typeof(State), propertyData.Value.ToString().Replace(" ", ""), true);
break;
case "Status":
newService.Status = propertyData.Value.ToString();
break;
case "SystemCreationClassName":
newService.SystemCreationClassName = propertyData.Value.ToString();
break;
case "SystemName":
newService.SystemName = propertyData.Value.ToString();
break;
case "TagId":
int.TryParse(propertyData.Value.ToString(), out newService.TagId);
break;
case "WaitHint":
int.TryParse(propertyData.Value.ToString(), out newService.WaitHint);
break;
}
}
// ensure newService is a valid instance -CT
if (!string.IsNullOrEmpty(newService.DisplayName))
allServices.Add(newService);
}
List<WmiService> filteredServices = new List<WmiService>();
if(filterString != null && filterString != String.Empty)
{
DataTable allServicesDataTable = new Business.DataTableConverter.DataTableConverter<WmiService>().GetDataTable(allServices);
DataView allServicesView = new DataView(allServicesDataTable);
allServicesView.RowFilter = filterString;
foreach (DataRowView allServicesRow in allServicesView)
filteredServices.Add((WmiService) allServicesRow["OriginalSourceObject"]);
}
else
{
filteredServices = allServices;
}
return filteredServices;
}
public static ReturnValue CreateOrUpdate(string machineName, string impersonationUsername, string impersonationPassword, string serviceName, string displayName, string serviceLocation, StartMode startMode, string username, string password)
{
List<WmiService> servicesToChange = WmiService.GetAllServices(machineName, impersonationUsername, impersonationPassword, "Name = '" + serviceName + "'");
if (servicesToChange.Count > 0)
{
ReturnValue result;
WmiService serviceToChange = servicesToChange[0];
bool requiresRestart = false;
if (serviceToChange.State != State.Stopped)
{
requiresRestart = true;
result = WmiService.Stop(machineName, impersonationUsername, impersonationPassword, serviceName);
if (result != ReturnValue.Success)
throw new Exception("Failed to stop " + displayName + " on: " + machineName + ", return code: " + result.ToString());
}
object[] parameters = new object[]
{
displayName, // DisplayName
serviceLocation, // Location
Convert.ToInt32(ServiceType.OwnProcess), // ServiceType
Convert.ToInt32(ErrorControl.Normal), // Error Control
GetStartModeString(startMode), // Start Mode
false, // Desktop Interaction
username, // Username
password, // Password
null, // Service Order Group
null, // Load Order Dependencies
null // Service dependencies
};
result = (ReturnValue) Convert.ToInt32(serviceToChange.WmiServiceObject.InvokeMethod("Change", parameters));
if (result != ReturnValue.Success)
throw new Exception("Failed to update " + displayName + " on: " + machineName + ", return code: " + result.ToString());
Thread.Sleep(3000);
if (requiresRestart == true)
{
result = WmiService.Start(machineName, impersonationUsername, impersonationPassword, serviceName);
if (result != ReturnValue.Success)
throw new Exception("Failed to restart " + displayName + " on: " + machineName + ", return code: " + result.ToString());
}
return result;
}
else
{
return Create(machineName, impersonationUsername, impersonationPassword, serviceName, displayName, serviceLocation, startMode, username, password);
}
}
#endregion
public static string GetStartModeString(StartMode startMode)
{
switch(startMode)
{
case StartMode.Auto:
return "Automatic";
}
return startMode.ToString();
}
}
}