user12510497
user12510497

Reputation:

C# Active Directory Search

I have this powershell function and i want to make it as a C# function. How can i put it into C#?

Get-ADComputer -filter {Name -Like 'myComp'} -property * | select DistinguishedName

Upvotes: 1

Views: 1063

Answers (4)

Gabriel Luci
Gabriel Luci

Reputation: 40858

All the other answers suggest using the System.DirectoryServices.AccountManagement namespace. While that will work, it is really just a wrapper around the System.DirectoryServices namespace to make things a bit easier to use. It does make things easier (sometimes) but it does so at the cost of performance.

For example, in all the examples you've been given, your code will retrieve every attribute with a value from the computer object in AD, even though you only want one attribute.

If you use DirectorySearcher, you can make the search and retrieve only that one attribute that you want:

public string GetComputerDn(string computerName) {
    var searcher = new DirectorySearcher {
        Filter = $"(&(objectClass=computer)(sAMAccountName={computerName}$))",
        PropertiesToLoad = { "distinguishedName" } //return only the distinguishedName attribute
    };
    var result = searcher.FindOne();
    if (result == null) return null;
    return (string) result.Properties["distinguishedName"][0];
}

Note that in AD, the sAMAccountName of computer objects is what you would normally refer to as the "computer name", followed by $, which is why the filter is what it is.

Upvotes: 0

Jawad
Jawad

Reputation: 11364

You can use C# in the following manner

  1. Connect to the Domain controller and get the DomainContext
  2. Use that to look up the computer objects based on a name.
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, Environment.UserDomainName) 
{
    using (PrincipalSearcher srch = new PrincipalSearcher(new ComputerPrincipal(ctx) { Name = "ServerName"}))
    {
        return srch.FindAll().Cast<ComputerPrincipal>().ToList().Select(x => x.DistinguishedName);
    }
}

Above returns a list of DistinguishedNames that matches the Server Name.

Upvotes: 0

sam
sam

Reputation: 1985

Please try this:

Add reference to Active Directory Services (%programfiles%\Reference Assemblies\Microsoft\Framework.NETFramework\\System.DirectoryServices.AccountManagement.dll)

public string GetComputerName(string computerName)
{
    using (var context = new PrincipalContext(ContextType.Domain, "your domain name goes here"))
    {
        using (var group = GroupPrincipal.FindByIdentity(context, "Active Directory Group Name goes here"))
        {
            var computers = @group.GetMembers(true);
            return computers.FirstOrDefault(c => c.Name == computerName).DistinguishedName;
        }
    }

    return null; // or return "Not Found"
}

Upvotes: -1

marc_s
marc_s

Reputation: 754220

You should be able to do this quite easily. Add a reference to System.DirectoryServices.AccountManagement and then use this code:

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, 'YourDomain'))
{
    ComputerPrincipal computer = ComputerPrincipal.FindByIdentity (ctx, "name");

    if (computer != null)
    {
        // do whatever you need to do with your computer principal
        string distinguishedName = computer.DistinguishedName;
    }

}

Update: if you don't know your domain ........ - you can also use:

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))

in which case the principal context is created for the current domain you're located in.

Upvotes: 2

Related Questions