题
如何在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" };
现在检索结果,并取出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 = { "objectClass", "cn", "distinguishedName", "instanceType", "whenCreated",
"whenChanged", "displayName", "uSNCreated", "uSNChanged", "showInAdvancedViewOnly",
"name", "objectGUID", "flags", "versionNumber", "systemFlags", "objectCategory",
"isCriticalSystemObject", "gPCFunctionalityVersion", "gPCFileSysPath",
"gPCMachineExtensionNames", "dSCorePropagationData", "nTSecurityDescriptor" };
现在,使用目录搜索者检索GPO。您将在结果中获得一个DirectoryEntry,其中包含属性集合中的所有字段。有些是com对象,因此您必须适当处理这些对象。
这是一个更好,更完整的示例。
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/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