This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
WindowsFeatures.cs
101 lines (82 loc) · 2.93 KB
/
WindowsFeatures.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
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
namespace IisLogRotator
{
/// <summary>
/// Windows features detection helper
/// </summary>
/// <seealso cref="https://msdn.microsoft.com/en-us/library/ee309383(v=vs.85).aspx"/>
internal class WindowsFeatures
{
private WindowsFeatures()
{
}
internal bool IisWebServerRole { get; private set; }
internal bool IisWebServer { get; private set; }
internal bool Iis6ManagementCompatibility { get; private set; }
public bool IisFtpServer { get; private set; }
public bool IisFtpSvc { get; private set; }
internal static WindowsFeatures GetFeatures()
{
ManagementScope scope = new ManagementScope(@"\\localhost\root\cimv2");
// TODO detect windows server roles/features first (Win32_ServerFeature), then client features (Win32_OptionalFeature)
// https://msdn.microsoft.com/en-us/library/cc280268(v=vs.85).aspx
WqlObjectQuery query = new WqlObjectQuery(@"
SELECT
Name,
InstallState
FROM
Win32_OptionalFeature
WHERE
Name LIKE 'IIS%'
OR Name LIKE 'Smtpsvc%'
");
Dictionary<string, uint> features;
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
{
features = searcher
.Get()
.Cast<ManagementObject>()
.ToDictionary(
obj => (string)obj.GetPropertyValue("Name"),
obj => (uint)obj.GetPropertyValue("InstallState")
);
}
#if DEBUG
Debug.WriteLine("Detected IIS features:");
Debug.Indent();
foreach (var feature in features)
{
string state;
switch (feature.Value)
{
case 1: state = "enabled"; break;
case 2: state = "disabled"; break;
case 3: state = "absent"; break;
default: state = "unknown"; break;
}
Debug.WriteLine(
"{0} = {1}",
feature.Key,
state
);
}
Debug.Unindent();
#endif
return new WindowsFeatures()
{
IisWebServerRole = HasFeatureEnabled(features, "IIS-WebServerRole"),
IisWebServer = HasFeatureEnabled(features, "IIS-WebServer"),
Iis6ManagementCompatibility = HasFeatureEnabled(features, "IIS-IIS6ManagementCompatibility"),
IisFtpServer = HasFeatureEnabled(features, "IIS-FTPServer"),
IisFtpSvc = HasFeatureEnabled(features, "IIS-FTPSvc")
};
}
internal static bool HasFeatureEnabled(Dictionary<string, uint> features, string name)
{
return features.ContainsKey(name) ? (features[name] == 1) : false;
}
}
}