Monday, December 1, 2008

Assigning a New Group Policy Link to an OU

Assigns the Group Policy link Sales Policy to the Sales OU in Active Directory.

On Error Resume Next
Set objContainer = GetObject _
   ("LDAP://ou=Sales,dc=NA,dc=fabrikam,dc=com") 
 
strExistingGPLink = objContainer.Get("gPLink")
 
strGPODisplayName = "Sales Policy"
strGPOLinkOptions = 2
strNewGPLink = "[" & GetGPOADsPath & ";" & strGPOLinkOptions & "]"
 
objContainer.Put "gPLink", strExistingGPLink & strNewGPLink
objContainer.Put "gPOptions", "0"
 
objContainer.SetInfo
 
Function GetGPOADsPath
  Set objConnection = CreateObject("ADODB.Connection")  
  objConnection.Open "Provider=ADsDSOObject;"   
 
  Set objCommand = CreateObject("ADODB.Command")
  objCommand.ActiveConnection = objConnection
 
  objCommand.CommandText = _
  "<LDAP://cn=Policies,cn=System,dc=NA,dc=fabrikam,dc=com>;;" & _
  "distinguishedName,displayName;onelevel"
  Set objRecordSet = objCommand.Execute
 
  While Not objRecordSet.EOF
    If objRecordSet.Fields("displayName") = strGPODisplayName Then
      GetGPOADsPath = "LDAP://" & objRecordSet.Fields("distinguishedName")
      objConnection.Close
      Exit Function
    End If
    objRecordSet.MoveNext
  Wend
  objConnection.Close
End Function

This is a VB Script, this can be used by saving the file in .vbs file