0

I need to deploy my vb.net application via group policy. I found some information on how to do this here http://windowsdevcenter.com/pub/a/windows/2006/11/14/how-to-deploy-software-using-group-policy.html.

Now I need to do the same programmatically. Please suggest some link, document or tutorial on this.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Harsh
  • 3,683
  • 2
  • 25
  • 41
  • Any reason you can't just use GP GUI? – Bali C Apr 04 '12 at 13:40
  • I need an automated system for the process for my client. This is the reason i need a programatic way to deploy through Group policy or any login script. – Harsh Apr 04 '12 at 13:43
  • 1
    @GeekOnDemand this is SO much more complicated than you could imagine. I had to create a software installation gpo programatically and found out quickly that there is no real way to do it. You *CAN* use various libraries (GPMGMT) to create an empty GPO, but as far as setting the actual settings, no good. You either have to edit the registry (which won't always work depending on the GPO in question), or do what I did: Create the GPO manually, export it to an xml file, then in your code you can merge your XML with a GPO migration table to configure the settings. Ugly but it "works." Good luck. – Jimmy D Apr 04 '12 at 15:13
  • @JJD Thanks. you actually understood what I want. I would give this a try. For this i'll create a blank GPO through my code & then merge the xml of the pre-created gpo's xml file. Is it right? – Harsh Apr 05 '12 at 05:35
  • @GeekOnDemand yup that's it in a nutshell. If you Google "gpo migration table" it will point you in the right direction. The only thing you might have to do is edit the XML on the fly so that it contains the correct domain name before you merge it. I'll post an answer with some code snippets. – Jimmy D Apr 05 '12 at 13:05

2 Answers2

1

Well,

I don't think there is documented API to create GPO. You may create it manually and then use CreateGPOLink function to link it to OU's

Just found PS script that suppose to create new GPO's, thought it may be help for you http://blogs.technet.com/b/heyscriptingguy/archive/2009/02/11/how-can-i-create-new-group-policy-objects.aspx

I think your question is answered here C# linking group policy in AD

Community
  • 1
  • 1
Zilog
  • 468
  • 3
  • 7
1

Hope this helps. Like I said, it's ugly but it works. You'll probably have to install the Group Policy Management Console so you can add a reference to GPMGMTLib.dll. This is directly from my code so you'll have to play with it but it should get you going in the right direction:

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()

And this this is the function that creates the migration table. For my test I used test.domain but as you can see I replace this with the current domain before I merge the XML. Note that the XML must be utf-16 or this won't work.

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
Jimmy D
  • 5,282
  • 16
  • 54
  • 70