我该如何在AD域中遍历给定GPO(使用名称或GUID)中可用和/或已设置的设置?而无需使用PowerShell等导出到XML / HTML。
我正在使用C#(.NET 4.0)。
我该如何在AD域中遍历给定GPO(使用名称或GUID)中可用和/或已设置的设置?而无需使用PowerShell等导出到XML / HTML。
我正在使用C#(.NET 4.0)。
那个问题让我兴奋起来,所以我去研究了一下。所以加1。
我从上到下找到的解决方案中,最好的是:
我有一个类似的问题,不想下载和安装Microsoft GPO库(Microsoft.GroupPolicy.Management)。我希望使用System.DirectoryServices完成所有操作。需要一些挖掘,但是可以实现。
首先,使用DirectorySearcher检索容器。您需要先打开目录条目以将其传递给搜索器。您需要的筛选器是:
string filter = "(&" + "(objectClass=organizationalUnit)" + "(OU=" + container + "))";
你需要的属性名称是"gPLink",因此创建一个包含该属性的数组:
string[] requestProperties = { "gPLink" };
using (var searcher = new DirectorySearcher(directory, filter, properties, SearchScope.Subtree))
{
SearchResultCollection results = searcher.FindAll();
DirectoryEntry entry = results[0].GetDirectoryEntry();
string gpLink = entry.Properties["gPLink"].Value;
"[LDAP://cn={31B2F340-016D-11D2-945F-00C04FB984F9},cn=policies,cn=system,DC=Test,DC=Domain;0]"
string filter = "(&" +
"(objectClass=groupPolicyContainer)" +
"(cn={31B2F340-016D-11D2-945F-00C04FB984F9}))";
您需要创建一个包含以下内容的属性数组:
Properties = { "objectClass", "cn", "distinguishedName", "instanceType", "whenCreated",
"whenChanged", "displayName", "uSNCreated", "uSNChanged", "showInAdvancedViewOnly",
"name", "objectGUID", "flags", "versionNumber", "systemFlags", "objectCategory",
"isCriticalSystemObject", "gPCFunctionalityVersion", "gPCFileSysPath",
"gPCMachineExtensionNames", "dSCorePropagationData", "nTSecurityDescriptor" };
这里有一个比上面更好、更完整的例子。
class Program
{
static void Main(string[] args)
{
DirectoryEntry rootDse = new DirectoryEntry("LDAP://rootDSE");
DirectoryEntry root = new DirectoryEntry("GC://" + rootDse.Properties["defaultNamingContext"].Value.ToString());
DirectorySearcher searcher = new DirectorySearcher(root);
searcher.Filter = "(objectClass=groupPolicyContainer)";
foreach (SearchResult gpo in searcher.FindAll())
{
var gpoDesc = gpo.GetDirectoryEntry().Properties["distinguishedName"].Value.ToString();
Console.WriteLine($"GPO: {gpoDesc}");
DirectoryEntry gpoObject = new DirectoryEntry($"LDAP://{gpoDesc}");
try
{
Console.WriteLine($"DisplayName: {gpoObject.Properties["displayName"].Value.ToString()}");
}
catch
{
}
try
{
Console.WriteLine($"PCFileSysPath: {gpoObject.Properties["gPCFileSysPath"].Value.ToString()}");
}
catch
{
}
try
{
Console.WriteLine($"VersionNumber: {gpoObject.Properties["versionNumber"].Value.ToString()}");
}
catch
{
}
try
{
Console.WriteLine($"UserExtensionNames: {gpoObject.Properties["gPCUserExtensionNames"].Value.ToString()}");
}
catch
{
}
try
{
Console.WriteLine($"MachineExtensionNames: {gpoObject.Properties["gPCMachineExtensionNames"].Value.ToString()}");
}
catch
{
}
try
{
Console.WriteLine($"PCFunctionality: {gpoObject.Properties["gPCFunctionalityVersion"].Value.ToString()}");
}
catch
{
}
}
Console.ReadKey();
}
}
更新:工作副本。现在您可以使用C#读取和解析给定的GPO,而无需使用PowerShell或将任何内容写入磁盘。
using Microsoft.GroupPolicy;
var guid = new Guid("A7DE85DE-1234-F34D-99AD-5AFEDF7D7B4A");
var gpo = new GPDomain("Centoso.local");
var gpoData = gpo.GetGpo(guid);
var gpoXmlReport = gpoData.GenerateReport(ReportType.Xml).ToString();
using (XmlReader reader = XmlReader.Create(new StringReader(gpoXmlReport)))
{
string field;
while (reader.MoveToNextAttribute())
{
foreach (string attr in attributes)
{
// do something
}
}
}
这里使用了组策略管理控制台(GPMC)工具: https://msdn.microsoft.com/zh-cn/library/windows/desktop/aa814316(v=vs.85).aspx
Microsoft.GroupPolicy 命名空间 https://msdn.microsoft.com/zh-cn/library/windows/desktop/microsoft.grouppolicy(v=vs.85).aspx