我需要通过组策略部署我的vb.net应用程序。我在http://windowsdevcenter.com/pub/a/windows/2006/11/14/how-to-deploy-software-using-group-policy.html找到了一些有关如何执行此操作的信息。
现在我需要以编程方式执行相同操作。 请提供一些链接,文档或教程。
答案 0 :(得分:1)
好吧,
我不认为有创建GPO的文档API。您可以手动创建它,然后使用CreateGPOLink功能将其链接到OU的
刚刚找到假设创建新GPO的PS脚本,认为它对您有所帮助http://blogs.technet.com/b/heyscriptingguy/archive/2009/02/11/how-can-i-create-new-group-policy-objects.aspx
我认为您的问题已在C# linking group policy in AD
中得到解答答案 1 :(得分:1)
希望这会有所帮助。就像我说的那样,它很难看,但它有效。您可能必须安装组策略管理控制台,以便添加对GPMGMTLib.dll的引用。这是直接来自我的代码,所以你必须玩它,但它应该让你朝着正确的方向前进:
Dim GPM As New GPMGMTLib.GPM
Dim GPMConst As GPMGMTLib.GPMConstants = GPM.GetConstants
Dim GPMDomain As GPMGMTLib.GPMDomain = GPM.GetDomain(Environment.GetEnvironmentVariable("USERDNSDOMAIN"), "", GPMConst.UseAnyDC)
Dim RootDSE As New DirectoryServices.DirectoryEntry("LDAP://RootDSE")
'Dim GPMSOM As GPMGMTLib.GPMSOM = GPMDomain.GetSOM("OU name") 'to link to specific OU
Dim GPMSOM As GPMGMTLib.GPMSOM = GPMDomain.GetSOM(RootDSE.Properties("defaultNamingContext").Value.ToString()) '//DC=domain,DC=test
'//=======================
'//see if we already exist
'//=======================
Dim GPMSearchExisting As GPMGMTLib.GPMSearchCriteria = GPM.CreateSearchCriteria
GPMSearchExisting.Add(GPMConst.SearchPropertyGPODisplayName, GPMGMTLib.GPMSearchOperation.opEquals, "Agent_Installation")
Dim GPOListExisting As GPMGMTLib.GPMGPOCollection = GPMDomain.SearchGPOs(GPMSearchExisting)
If GPOListExisting.Count <> 0 Then
MsgBox("GPO already exists.")
Exit Sub
End If
'//=============================================================================
'//copy compressed GPO template from embedded resources to filesystem then unzip
'//=============================================================================
lblStatus.Text += "Copying embedded GPO template to filesystem..." & vbNewLine
lblStatus.Refresh()
My.Computer.FileSystem.WriteAllBytes("c:\Agent_Installation_GPO.zip", My.Resources.Agent_Installation_GPO, False)
lblStatus.Text += "Extracting GPO template from archive..." & vbNewLine
lblStatus.Refresh()
Call UnZip("c:\Agent_Installation_GPO.zip", "c:\")
'//=========================================================================================
'//need to create a GPO migration table on the fly. see Create_Migration_Table() for details
'//=========================================================================================
lblStatus.Text += "Creating GPO migration table..." & vbNewLine
lblStatus.Refresh()
Call Create_Migration_Table("c:\Agent_Installation_GPO.migtable")
lblStatus.Text += "Creating GPO..." & vbNewLine
lblStatus.Refresh()
Dim GPO As GPMGMTLib.GPMGPO = GPMDomain.CreateGPO
GPO.DisplayName = "Agent_Installation"
lblStatus.Text += "Linking GPO to domain..." & vbNewLine
lblStatus.Refresh()
'//===========================
'//links the GPO to the domain
'//===========================
GPMSOM.CreateGPOLink(-1, GPO)
Dim GPMSearchCriteria As GPMGMTLib.GPMSearchCriteria = GPM.CreateSearchCriteria
GPMSearchCriteria.Add(GPMConst.SearchPropertyGPODisplayName, GPMGMTLib.GPMSearchOperation.opEquals, "Agent_Installation")
Dim GPOList As GPMGMTLib.GPMGPOCollection = GPMDomain.SearchGPOs(GPMSearchCriteria)
Dim GPMGPO As GPMGMTLib.GPMGPO = GPOList.Item(1)
lblStatus.Text += "Importing settings from template..." & vbNewLine
lblStatus.Refresh()
'//========================================================
'//link migration table to template and import all settings
'//========================================================
Dim GPMBackupDir As GPMGMTLib.GPMBackupDir = GPM.GetBackupDir("C:\Agent_Installation_GPO")
Dim GPMBackup As GPMGMTLib.GPMBackup = GPMBackupDir.GetBackup("{193E0BEE-B37E-4472-A032-F297C4A5D8E1}")
Dim GPMMigrationTable As GPMGMTLib.GPMMigrationTable = GPM.GetMigrationTable("c:\Agent_Installation_GPO.migtable")
Dim GPMResult As GPMGMTLib.GPMResult = GPMGPO.Import(0, GPMBackup, GPMMigrationTable)
lblStatus.Text += "Done"
lblStatus.Refresh()
这就是创建迁移表的功能。对于我的测试,我使用了test.domain但是你可以看到我在合并XML之前用当前域替换它。请注意,XML必须是utf-16,否则无效。
Using objWriter As New System.IO.StreamWriter(strPath, False, System.Text.Encoding.Unicode) '//must be utf-16
objWriter.WriteLine("<?xml version=""1.0"" encoding=""utf-16""?>")
objWriter.WriteLine("<MigrationTable xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://www.microsoft.com/GroupPolicy/GPOOperations/MigrationTable"">")
objWriter.WriteLine(" <Mapping>")
objWriter.WriteLine(" <Type>UNCPath</Type>")
objWriter.WriteLine(" <Source>\\test.domain\netlogon</Source>")
objWriter.WriteLine(" <Destination>\\" & Environment.GetEnvironmentVariable("USERDNSDOMAIN") & "\netlogon</Destination>")
objWriter.WriteLine(" </Mapping>")
objWriter.Write("</MigrationTable>")
objWriter.Close()
End Using