使用C#读取组策略设置

时间:2011-03-15 23:35:56

标签: c# c#-4.0 active-directory gpo

如何在AD域中迭代给定GPO中的可用和/或设置设置(使用名称或GUID)?无需使用PowerShell等导出到XML / HTML

我正在使用C#(.NET 4.0)。

4 个答案:

答案 0 :(得分:8)

这个问题让我大肆宣传,所以我去研究它。所以一个+1

我从顶部发现的一些解决方案是最好的,最底层是最差的

答案 1 :(得分:3)

我遇到了类似的问题,并且不想下载和安装Microsoft GPO库(Microsoft.GroupPolicy.Management)。我想用System.DirectoryServices完成所有操作。它需要一点挖掘,但它可以完成。

首先使用DirectorySearcher检索容器。您需要已经打开了一个目录条目以传递给搜索者。您想要的过滤器是:

string filter = "(&" + "(objectClass=organizationalUnit)" + "(OU=" + container + "))";

并且您感兴趣的属性名为" gPLink",因此在其中创建一个包含该属性的数组:

string[] requestProperties = { "gPLink" };

现在检索结果,然后拔出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;

如果gpLink为null,则表示没有与容器(OU)关联的GPO。 否则,gpLink将包含如下字符串:

"[LDAP://cn={31B2F340-016D-11D2-945F-00C04FB984F9},cn=policies,cn=system,DC=Test,DC=Domain;0]"

在上面的文本中,您可以看到GPO的CN。我们现在需要做的就是从DC中检索GPO。

为此,我们使用如下所示的过滤器:

string filter = "(&" +
    "(objectClass=groupPolicyContainer)" +
    "(cn={31B2F340-016D-11D2-945F-00C04FB984F9}))";

您想要创建一个包含以下内容的Properties数组:

Properties = { "objectClass", "cn", "distinguishedName", "instanceType", "whenCreated",
    "whenChanged", "displayName", "uSNCreated", "uSNChanged", "showInAdvancedViewOnly",
    "name", "objectGUID", "flags", "versionNumber", "systemFlags", "objectCategory", 
    "isCriticalSystemObject", "gPCFunctionalityVersion", "gPCFileSysPath",
    "gPCMachineExtensionNames", "dSCorePropagationData", "nTSecurityDescriptor" };

现在使用DirectorySearcher检索GPO。您将在包含Properties集合中所有上述字段的结果中返回DirectoryEntry。有些是COM对象,因此您必须妥善处理这些对象。

答案 2 :(得分:0)

更新:工作副本。您现在可以使用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/en-us/library/windows/desktop/aa814316(v=vs.85).aspx

Microsoft.GroupPolicy命名空间 https://msdn.microsoft.com/en-us/library/windows/desktop/microsoft.grouppolicy(v=vs.85).aspx

答案 3 :(得分:0)

以上是一个更好,更完整的例子。

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();
    }
}