2012-05-10 79 views
0

我正在尋找一種方法,通過代碼列出應用(鏈接和繼承)到特定OU的所有GPO。如何列出應用於包含繼承的OU的所有GPO

我知道有一個屬性表示鏈接的GPO,稱爲GPLink,但它只給出了直接鏈接的GPO。

我搜索谷歌,發現有一個gpmgmt COM對象,但我不明白如何使用它爲我的目的,如果它甚至可能。

感謝您的任何幫助。

回答

0

您需要向上遍歷給定OU的每個父項,直到到達域頭。

1

我有以下的分享。它沒有列出GPO的名稱,但返回了計數。小模塊將允許您獲取名稱(在foreach循環中查看GPOLink的屬性)。您將需要安裝GPMC並將gpmgmt.dll添加爲項目引用。

private string getGPOLinkCount(string OUPathDN, bool onlyEnabledLinks, bool includeInheritedLinks) 
    { 
     int linkCount = 0; 

     try 
     { 
      GPMGMTLib.GPM gpm = new GPMGMTLib.GPM(); 
      GPMGMTLib.IGPMConstants gpc = gpm.GetConstants(); 
      GPMGMTLib.IGPMDomain gpd = gpm.GetDomain(Environment.GetEnvironmentVariable("USERDNSDOMAIN"), "", gpc.UseAnyDC); 

      GPMGMTLib.GPMSOM gpSom = gpd.GetSOM(OUPathDN); 

      GPMGPOLinksCollection GPOLinks = gpSom.GetGPOLinks(); 
      GPMGPOLinksCollection GPOLinksIncludingInherited = gpSom.GetInheritedGPOLinks(); 


      if (!includeInheritedLinks) 
      { 
       foreach (GPMGPOLink GPOLink in GPOLinks) 
       { 
        if (onlyEnabledLinks) 
        { 
         if (GPOLink.Enabled) 
         { 
          linkCount++; 
         } 
        } 
        if (!onlyEnabledLinks) //Get all links, disabled or enabled 
        { 
         linkCount++; 
        } 
       }     
      } 

      if (includeInheritedLinks) 
      { 
       foreach (GPMGPOLink GPOLink in GPOLinksIncludingInherited) 
       { 
        if (onlyEnabledLinks) 
        { 
         if (GPOLink.Enabled) 
         { 
          linkCount++; 
         } 
        } 
        if (!onlyEnabledLinks) //Get all links, disabled or enabled 
        { 
         linkCount++; 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      return "GPO links: " + ex.Message.Replace("\r\n", ""); 
     } 

     return linkCount.ToString();    
    } 
相關問題