1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| ' ------ SCRIPT CONFIGURATION -----
strDomainDN = "<DomainDN>" ' e.g. dc=adatum,dc=com
strGPO = "<GPOName>" ' e.g. WorkstationsGPO
strOUDN = "<OrgUnitDN>" ' e.g. ou=Workstations,dc=adatum,dc=com
' ------ END CONFIGURATION --------
strBaseDN = "<LDAP://cn=policies,cn=system,dc=" & strDomainDN & ">;"
strFilter = "(&(objectcategory=grouppolicycontainer)" & _
"(objectclass=grouppolicycontainer)" & _
"(displayname=" & strGPO & "));"
strAttrs = "ADsPath;"
strScope = "OneLevel"
set objConn = CreateObject("ADODB.Connection")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"
set objRS = objConn.Execute(strBaseDN & strFilter & strAttrs & strScope)
if objRS.EOF <> TRUE then
objRS.MoveFirst
end if
if objRS.RecordCount = 1 then
strGPOADsPath = objRS.Fields(0).Value
WScript.Echo "GPO Found: " & strGPOADsPath
elseif objRS.RecordCount = 0 then
WScript.Echo "Did not founding matching GPO for: " & strGPO
Wscript.Quit
elseif objRS.RecordCount > 1 then
WScript.Echo "More than 1 GPO found matching: " & strGPO
Wscript.Quit
end if
set objOU = GetObject("LDAP://" & strOUDN)
on error resume next
strGPLink = objOU.Get("gpLink")
if Err.Number then
if Err.Number <> -2147463155 then
WScript.Echo "Fatal error while retrieving gpLink attribute: " & _
Err.Description
Wscript.Quit
end if
end if
on error goto 0
objOU.Put "gpLink", strGPLink & "[" & strGPOADsPath & ";0]"
objOU.SetInfo
WScript.Echo "GPO successfully linked"
' The following code segment will remove any GPOs that
' are linked to an OU
Const ADS_PROPERTY_CLEAR = 1
Set objContainer = GetObject _
("LDAP://<OU DN>") ' i.e. "ou=Finance,dc=adatum,dc=com"
objContainer.PutEx ADS_PROPERTY_CLEAR, "gPLink", 0
objContainer.PutEx ADS_PROPERTY_CLEAR, "gPOptions", 0
objContainer.SetInfo |
Partager