0

我需要通过组策略部署我的 vb.net 应用程序。我在这里找到了一些关于如何做到这一点的信息http://windowsdevcenter.com/pub/a/windows/2006/11/14/how-to-deploy-software-using-group-policy.html

现在我需要以编程方式做同样的事情。请就此提出一些链接、文档或教程。

4

2 回答 2

1

出色地,

我认为没有记录在案的 API 可以创建 GPO。您可以手动创建它,然后使用CreateGPOLink函数将其链接到 OU 的

刚刚找到了假设创建新 GPO 的 PS 脚本,认为它可能对您有所帮助 http://blogs.technet.com/b/heyscriptingguy/archive/2009/02/11/how-can-i-create-new-组策略对象.aspx

我认为您的问题在这里得到了回答C# linking group policy in AD

于 2012-04-04T14:11:25.583 回答
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
于 2012-04-05T13:40:58.827 回答