C#连接在AD组策略 [英] C# linking group policy in AD
问题描述
如何设置AD组策略?我能创造我的OU,但我还需要附上组策略链接到它。因此,这是我到目前为止所。
字符串strOU =OU = test454545,OU =客户,OU =客户端,DC =域,DC =净;
GPMGMTLib.GPM加仑=新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,anyt想法?并感谢您的帮助。
好吧越来越近了,这只是创造了一个政策实际上并没有连接现有的...
字符串strGPO =默认使用网络访问安全性;
字符串strOU =OU = test454545,OU =客户,OU =客户端,DC =域,DC =净;
GPMGMTLib.GPM加仑=新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);
更新和工作:
这是用于连接现有GPO的到OU的使用C#
1)安装<一href="http://www.microsoft.com/downloads/en/confirmation.aspx?FamilyID=0a6d4c24-8cbd-4b35-9272-dd3cbfc81887" rel="nofollow">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;项目
字符串strGPO =默认使用网络访问安全性;
字符串strOU =OU = test454545,OU =客户,OU =用户,DC = domainh,DC =净;
GPMGMTLib.GPM加仑=新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]);
看看这个<一href="http://www.microsoft.com/downloads/en/details.aspx?FamilyId=38C1A89B-A6D2-4F2A-A944-9236999AEE65&displaylang=en"相对=nofollow>链接
它含有大量示例脚本,您将需要一个参考(COM)加载到GPO管理员1.0类型库从GPOAdmin.dll。
有讨论过类似的问题,<一个href="http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/f3f5a61f-2ab9-459e-a1ee-c187465198e0"相对=nofollow>此处在C#示例脚本
修改
参考gpmgmt.dll为COM互操作,并使用code如下:
公共功能CreateAndLinkGPO(BYVAL strDomain作为字符串,BYVAL strOU作为字符串,BYVAL strGPOName作为字符串)
昏暗的GPM作为新GPM()
昏暗gpmConst作为GPMConstants = gpm.GetConstants()
昏暗域GPMDomain = gpm.GetDomain(strDomain,,gpmConst.UseAnyDC)
昏暗的索姆作为GPMSOM = domain.GetSOM(strOU)
创建新的GPO
昏暗GPO作为GPMGPO = domain.CreateGPO()
gpo.DisplayName = strGPOName
创建链接到OU
som.CreateGPOLink(-1,GPO)
CreateAndLinkGPO = GPO
端功能
这是在VB.NET,但可以很容易地移植到C#由... <一个href="http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/fcbd4eae-96c7-4a5f-8e5a-cf5b8a2542c7/"相对=nofollow>从这里 MSFT海报。我认为关键是。 CreateGPOLink , GPMSOM 是你的OU(检索该IGPMSOM界面重新presents在指定的路径域或组织单位(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);
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);
Update and WORKING:
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]);
Take a look at this link
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.
There is a similar issue discussed here with a sample script in C#
EDIT:
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
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组策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!