' This VBScript code links a GPO to an OU ' --------------------------------------------------------------- ' From the book "Active Directory Cookbook" by Robbie Allen ' Publisher: O'Reilly and Associates ' ISBN: 0-596-00466-4 ' Book web site: http://rallenhome.com/books/adcookbook/code.html ' --------------------------------------------------------------- ' ------ SCRIPT CONFIGURATION ------ strGPO = "Vorlage" ' e.g. Sales GPO strDomain = "gpo-5.local" ' e.g. rallencorp.com strOU = "ou=MeineFirma,dc=gpo-5,dc=local" ' e.g. ou=Sales,dc=rallencorp,dc=com intLinkPos = -1 ' set this to the position the GPO evaluated at ' a value of –1 signifies appending it to the end of the list ' ------ END CONFIGURATION --------- set objGPM = CreateObject("GPMgmt.GPM") set objGPMConstants = objGPM.GetConstants() ' Initialize the Domain object set objGPMDomain = objGPM.GetDomain(strDomain, "", objGPMConstants.UseAnyDC) ' Find the specified GPO set objGPMSearchCriteria = objGPM.CreateSearchCriteria objGPMSearchCriteria.Add objGPMConstants.SearchPropertyGPODisplayName, objGPMConstants.SearchOpEquals, cstr(strGPO) set objGPOList = objGPMDomain.SearchGPOs(objGPMSearchCriteria) if objGPOList.Count = 0 then WScript.Echo "Did not find GPO: " & strGPO WScript.Echo "Exiting." WScript.Quit elseif objGPOList.Count > 1 then WScript.Echo "Found more than one matching GPO. Count: " & _ objGPOList.Count WScript.Echo "Exiting." WScript.Quit ' else ' WScript.Echo "Found GPO: " & objGPOList.Item(1).DisplayName end if ' Find the specified OU set objSOM = objGPMDomain.GetSOM(strOU) if IsNull(objSOM) then WScript.Echo "Did not find OU: " & strOU WScript.Echo "Exiting." WScript.Quit ' else ' WScript.Echo "Found OU: " & objSOM.Name end if on error resume next set objGPMLink = objSOM.CreateGPOLink( intLinkPos, objGPOList.Item(1) ) if Err.Number <> 0 then WScript.Echo "There was an error creating the GPO link." WScript.Echo "Error: " & Err.Description ' else ' WScript.Echo "Sucessfully linked GPO to OU" end if