DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 9.11 Listing the Links for GPO

9.11.1 Problem

You want to list all of the links for a particular GPO.

9.11.2 Solution

9.11.2.1 Using a graphical user interface
  1. Open the GPMC snap-in.

  2. In the left pane, expand the Forest container, expand the Domains container, browse to the domain of the target GPO, and expand the Group Policy Objects container.

  3. Click on the GPO you want to view the links for.

  4. In the right pane, the defined links for the GPO will be listed under Links.

9.11.2.2 Using a command-line interface
> dumpgpoinfo.wsf "<GPOName>"
9.11.2.3 Using VBScript
' This code lists all the sites, OUs, and domains a GPO is linked to.
' ------ SCRIPT CONFIGURATION ------
strGPO      = "<GPOName>"        ' e.g. SalesGPO
strForest   = "<ForestName>"     ' e.g. rallencorp.com
strDomain   = "<DomainDNSName>"  ' e.g. rallencorp.com
' ------ END CONFIGURATION ---------

set objGPM = CreateObject("GPMgmt.GPM")
set objGPMConstants = objGPM.GetConstants( )
  
' Initialize the Domain object
set objGPMDomain = objGPM.GetDomain(strDomain, "", objGPMConstants.UseAnyDC)
' Initialize the Sites Container object
set objGPMSitesContainer = objGPM.GetSitesContainer(strForest, _
                           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

' Search for all SOM links for this GPO
set objGPMSearchCriteria = objGPM.CreateSearchCriteria
objGPMSearchCriteria.Add objGPMConstants.SearchPropertySOMLinks, _
                         objGPMConstants.SearchOpContains, objGPOList.Item(1)
set objSOMList = objGPMDomain.SearchSOMs(objGPMSearchCriteria)
set objSiteLinkList = objGPMSitesContainer.SearchSites(objGPMSearchCriteria)

if objSOMList.Count = 0 and objSiteLinkList.Count = 0 Then
   WScript.Echo "No Site, Domain, or OU links found for this GPO"
else
   WScript.Echo "Links:"
   for each objSOM in objSOMList
      select case objSOM.Type
         case objGPMConstants.SOMDomain
            strSOMType = "Domain"
         case objGPMConstants.SOMOU
            strSOMType = "OU"
      end select
      ' Print GPO Domain and OU links
      WScript.Echo "  " & objSOM.Name & " (" & strSOMType & ")"   
   next

   ' Print GPO Site Links
   for each objSiteLink in objSiteLinkList
      WScript.Echo "  " & objSiteLink.Name & " (Site)"
   next
end if

9.11.3 Discussion

See the Introduction in Chapter 9 for more information on GPO linking and SOMs.

9.11.3.1 Using VBScript

First, I have to find the target GPO. To do this, I use a GPMSearchCriteria object to find the GPO that is equal to the display name of the GPO specified in the configuration section. I use an if elseif else conditional statement to ensure that only one GPO is returned. If none or more than one are returned, I abort the script. If only one is returned, I search for all SOMs (domain, OUs, and sites) that have the GPO linked using the GPMSitesContainer.SearchSites and GPMDomain.SearchSOMs methods.

9.11.4 See Also

Recipe 9.12 for creating a GPO link to an OU MSDN: GPMDomain.SearchSOMs, and MSDN: GPMSitesContainer.SearchSites

    [ Team LiB ] Previous Section Next Section