C# 链接 AD 中的组策略

C# linking group policy in AD(C# 链接 AD 中的组策略)
本文介绍了C# 链接 AD 中的组策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何在 AD 中设置组策略?我可以创建我的 OU,但我还需要附加组策略链接到它.所以这就是我目前所拥有的.

How do I set group policies in AD? I'm able to create my OU but i also need to attach group policy linking to it. So this is what i have so far.

 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,有什么想法吗?并感谢您的帮助.

This still doesn't create the GPO link, but all i want to do is link an existing GPO, anyt thoughts? And thanks for the help.

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

Okay getting closer, this just created a policy doesn't actually link an existing one...

 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;项目

This is for linking existing GPO's to OU's using C#
1) install http://www.microsoft.com/downloads/en/confirmation.aspx?FamilyID=0a6d4c24-8cbd-4b35-9272-dd3cbfc81887
2) Reference gpmgmt.dll (found in the install directory)
3) You might have to install .Net 1.1
4) Add References to VS
5) add using GPMGMTLib; using GPOADMINLib; to project

            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]);

推荐答案

看看这个 链接

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

It contains a lot of sample scripts, you will need to add a reference (COM) to GPO Admin 1.0 Type Library from GPOAdmin.dll.

讨论了一个类似的问题 此处 使用 C# 中的示例脚本

There is a similar issue discussed here with a sample script in C#

编辑:

引用 gpmgmt.dll 作为 COM interop 并使用如下代码:

Reference gpmgmt.dll as COM interop and use the code as below:

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 海报.我认为关键是 .CreateGPOLink, GPMSOM 是您的 OU(检索表示指定路径上的域或组织单位 (OU) 的 IGPMSOM 接口.)

This is in VB.NET, but can be easily ported to C# posted by a MSFT poster from here. I think the key is .CreateGPOLink, GPMSOM is your OU (Retrieves the IGPMSOM interface that represents the domain or the organizational unit (OU) at the specified path.)

这篇关于C# 链接 AD 中的组策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

What#39;s the difference between retrieving WindowsPrincipal from WindowsIdentity and Thread.CurrentPrincipal?(从 WindowsIdentity 和 Thread.CurrentPrincipal 检索 WindowsPrincipal 之间有什么区别?)
How do I find a user#39;s Active Directory display name in a C# web application?(如何在 C# Web 应用程序中查找用户的 Active Directory 显示名称?)
How to use Servicestack Authentication with Active Directory/Windows Authentication?(如何在 Active Directory/Windows 身份验证中使用 Servicestack 身份验证?)
How can I authenticate against Active Directory in Nancy?(如何在 Nancy 中对 Active Directory 进行身份验证?)
How to get a username in Active Directory from a display name in C#?(如何从 C# 中的显示名称获取 Active Directory 中的用户名?)
Oauth 2 token for Active Directory accounts(Active Directory 帐户的 Oauth 2 令牌)