C# linking group policy in AD

Take a look at this link

It contains a lot of sample scripts, you will need to add a reference (COM) to GPO Admin 1.0 Type Library from GPOAdmin.dll.

There is a similar issue discussed here with a sample script in C#

EDIT:

Reference gpmgmt.dll as COM interop and use the code as below:

Public Function CreateAndLinkGPO(ByVal strDomain As String, ByVal strOU As String, ByVal strGPOName As String)
    Dim gpm As New GPM()
    Dim gpmConst As GPMConstants = gpm.GetConstants()
    Dim domain As GPMDomain = gpm.GetDomain(strDomain, "", gpmConst.UseAnyDC)
    Dim som As GPMSOM = domain.GetSOM(strOU)

    'create new GPO
    Dim gpo As GPMGPO = domain.CreateGPO()
    gpo.DisplayName = strGPOName 

    'create link to OU
    som.CreateGPOLink(-1, gpo)

    CreateAndLinkGPO = gpo
End Function

This is in VB.NET, but can be easily ported to C# posted by a MSFT poster from here. I think the key is .CreateGPOLink, GPMSOM is your OU (Retrieves the IGPMSOM interface that represents the domain or the organizational unit (OU) at the specified path.)


I was looking for a way to simply list the GPOs that are linked to a particular OU, this thread helped me tremendously. I have the following sub to share. It doesn't list the names of the GPOs but returns the count. A minor mod will allow you to get the names (check out the properties of GPOLink in the foreach loop). You will need to have the GPMC installed and the gpmgmt.dll added as a project reference.

 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();            
    }

Comments

  1. Bruno

    • 2020/11/16

    C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on.

  2. Remi

    • 2018/6/3

    C (/ ˈ s iː /, as in the letter c) is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system. By design, C provides constructs that map efficiently to typical machine instructions.

  3. Donald

    • 2020/7/5

    C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone 

  4. Christopher

    • 2018/6/28

    1 Day C -4.75% DJIA -2.19% S&P 500 -2.17% Financial Services -0.98% The Price to Earnings (P/E) ratio, a key valuation measure, is calculated by dividing the stock's most recent closing price by

  5. Russo

    • 2019/3/5

    learn-c.org is a free interactive C tutorial for people who want to learn C, fast.

  6. Johnathan

    • 2021/3/31

    Citigroup, Inc. Common Stock (C) Stock Quotes - Nasdaq offers stock quotes & market activity data for US and global markets.

  7. Everett

    • 2020/4/19

    C is considered as a middle-level language because it supports the feature of both low-level and high-level languages. C language program is converted into 

  8. Hernandez

    • 2017/3/13

    Discover historical prices for C stock on Yahoo Finance. View daily, weekly or monthly format back to when Citigroup, Inc. stock was issued.

  9. Jakob

    • 2016/6/29

    Amazon.com: C Programming Language, 2nd Edition: 8601410794231: Brian W. Kernighan, Dennis M. Ritchie: Books.

  10. Marvin

    • 2016/2/10

    C or Do is the first note of the C major scale, the third note of the A minor scale (the relative minor of C major), and the fourth note (G, A, B, C) of the Guidonian hand, commonly pitched around 261.63 Hz.

  11. Mehmeti

    • 2018/6/19

    C++ (/ ˌ s iː ˌ p l ʌ s ˈ p l ʌ s /) is a general-purpose programming language created by Bjarne Stroustrup as an extension of the C programming language, or "C with Classes".The language has expanded significantly over time, and modern C++ now has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

  12. Ben

    • 2019/5/17

    c = a + b; Here, ‘+’ is the operator known as addition operator and ‘a’ and ‘b’ are operands. The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’. C/C++ has many built-in operator types and they are classified as follows:

Comments are closed.

Recent Posts