C#链接AD中的组策略

时间:2011-03-15 13:07:24

标签: c# .net active-directory

如何在AD中设置组策略?我能够创建我的OU,但我还需要附加组策略链接到它。所以这就是我到目前为止所做的。

 string strOU = "OU=test454545,OU=Clients,OU=Clients,DC=domain,DC=net";
        GPMGMTLib.GPM gpm = new GPMGMTLib.GPM();
        GPMGMTLib.GPMConstants gpc = gpm.GetConstants();
        GPMGMTLib.GPMDomain gpd = gpm.GetDomain(Environment.GetEnvironmentVariable("USERDNSDOMAIN"), "", gpc.UseAnyDC);  
        GPMGMTLib.GPMSOM gpSom = gpd.GetSOM(strOU);

        GPMGMTLib.GPMGPO gpo = gpd.CreateGPO();
        gpo.DisplayName  = "TestOutCome";
        gpSom.CreateGPOLink(-1,gpo);

这仍然不会创建GPO链接,但我想要做的就是链接现有的GPO,不管怎么说?谢谢你的帮助。

好的接近,这只是创建了一个政策实际上并没有链接现有的...

 string strGPO = "Default Security with web access";
        string strOU = "OU=test454545,OU=Clients,OU=Clients,DC=domain,DC=net";
        GPMGMTLib.GPM gpm = new GPMGMTLib.GPM();
        GPMGMTLib.GPMConstants gpc = gpm.GetConstants();
        GPMGMTLib.GPMDomain gpd = gpm.GetDomain(Environment.GetEnvironmentVariable("USERDNSDOMAIN"), "", gpc.UseAnyDC);
        GPMGMTLib.GPMSearchCriteria searchOBJ = gpm.CreateSearchCriteria();
        searchOBJ.Add(gpc.SearchPropertyGPODisplayName, gpc.SearchOpEquals, strGPO);
        GPMGMTLib.GPMGPOCollection objGPOlist = gpd.SearchGPOs(searchOBJ);
        GPMGMTLib.GPMSOM gpSom = gpd.GetSOM(strOU);
        GPMGMTLib.GPMGPO gpo = gpd.CreateGPO();
        gpSom.CreateGPOLink(-1,gpo);

更新和工作:

这是用于使用C#
将现有GPO链接到OU 1)安装http://www.microsoft.com/downloads/en/confirmation.aspx?FamilyID=0a6d4c24-8cbd-4b35-9272-dd3cbfc81887
2)参考gpmgmt.dll(在安装目录中找到)
3)您可能必须安装.Net 1.1
4)将参考添加到VS
5)使用GPMGMTLib添加;使用GPOADMINLib;项目

            string strGPO = "Default Security with web access";
        string strOU = "OU=test454545,OU=Clients,OU=clients,DC=domainh,DC=net";
        GPMGMTLib.GPM gpm = new GPMGMTLib.GPM();
        GPMGMTLib.GPMConstants gpc = gpm.GetConstants();
        GPMGMTLib.GPMDomain gpd = gpm.GetDomain(Environment.GetEnvironmentVariable("USERDNSDOMAIN"), "", gpc.UseAnyDC);
        GPMGMTLib.GPMSearchCriteria searchOBJ = gpm.CreateSearchCriteria();
        searchOBJ.Add(gpc.SearchPropertyGPODisplayName, gpc.SearchOpEquals, strGPO);
        GPMGMTLib.GPMGPOCollection objGPOlist = gpd.SearchGPOs(searchOBJ);
        GPMGMTLib.GPMSOM gpSom = gpd.GetSOM(strOU);
        GPMGMTLib.GPMGPO gpo = gpd.CreateGPO();
        gpSom.CreateGPOLink(-1,objGPOlist[1]);

2 个答案:

答案 0 :(得分:4)

看看这个link

它包含大量示例脚本,您需要从GPOAdmin.dll向GPO Admin 1.0类型库添加引用(COM)。

在C#

中使用示例脚本讨论了here类似的问题

编辑

将gpmgmt.dll作为COM互操作引用,并使用如下代码:

Public Function CreateAndLinkGPO(ByVal strDomain As String, ByVal strOU As String, ByVal strGPOName As String)
    Dim gpm As New GPM()
    Dim gpmConst As GPMConstants = gpm.GetConstants()
    Dim domain As GPMDomain = gpm.GetDomain(strDomain, "", gpmConst.UseAnyDC)
    Dim som As GPMSOM = domain.GetSOM(strOU)

    'create new GPO
    Dim gpo As GPMGPO = domain.CreateGPO()
    gpo.DisplayName = strGPOName 

    'create link to OU
    som.CreateGPOLink(-1, gpo)

    CreateAndLinkGPO = gpo
End Function

这是在VB.NET中,但可以轻松移植到MSFT poster from here发布的C#。我认为关键是。CreateGPOLinkGPMSOM是您的OU(检索表示指定路径上的域或组织单位(OU)的IGPMSOM接口。)

答案 1 :(得分:2)

我一直在寻找一种方法来简单地列出链接到特定OU的GPO,这个线程对我有很大的帮助。我有以下分享。它不会列出GPO的名称,但会返回计数。一个小mod将允许您获取名称(查看foreach循环中 GPOLink 的属性)。您需要安装GPMC并添加gpmgmt.dll作为项目引用。

 private string getGPOLinkCount(string OUPathDN, bool onlyEnabledLinks, bool includeInheritedLinks)
    {
        int linkCount = 0;

        try
        {
            GPMGMTLib.GPM gpm = new GPMGMTLib.GPM();
            GPMGMTLib.IGPMConstants gpc = gpm.GetConstants();
            GPMGMTLib.IGPMDomain gpd = gpm.GetDomain(Environment.GetEnvironmentVariable("USERDNSDOMAIN"), "", gpc.UseAnyDC);

            GPMGMTLib.GPMSOM gpSom = gpd.GetSOM(OUPathDN);

            GPMGPOLinksCollection GPOLinks = gpSom.GetGPOLinks();
            GPMGPOLinksCollection GPOLinksIncludingInherited = gpSom.GetInheritedGPOLinks();


            if (!includeInheritedLinks)
            {
                foreach (GPMGPOLink GPOLink in GPOLinks)
                {
                    if (onlyEnabledLinks)
                    {
                        if (GPOLink.Enabled)
                        {
                            linkCount++;
                        }
                    }
                    if (!onlyEnabledLinks) //Get all links, disabled or enabled
                    {
                        linkCount++;
                    }
                }                   
            }

            if (includeInheritedLinks)
            {
                foreach (GPMGPOLink GPOLink in GPOLinksIncludingInherited)
                {
                    if (onlyEnabledLinks)
                    {
                        if (GPOLink.Enabled)
                        {
                            linkCount++;
                        }
                    }
                    if (!onlyEnabledLinks) //Get all links, disabled or enabled
                    {
                        linkCount++;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            return "GPO links: " + ex.Message.Replace("\r\n", "");
        }

        return linkCount.ToString();            
    }