1

I am trying to find the relevant C# API for Powershell's Get-ADDomain. I don't want to invoke the Powershell commands in C#. Instead I am looking for one or multiple C# API with which I can retrieve all the values.

Get-ADDomain -Identity user.com

I tried searching through DOT net API's but couldn't find a relevant one. I found Domain class. But I am not sure how to get all the info using GetDomain

System.DirectoryServices.ActiveDirectory.Domain.GetDomain(DirectoryContext)

Can someone help in finding the relevant C# API that I can use to retrieve all the values of Get-ADDomain?

1
  • I don't want to run a Powershell command. Instead I am trying to find C# APIs with which I get relevant information. Commented Feb 16, 2022 at 4:27

1 Answer 1

1

If you want to get the domain info for the domain that the computer is a member of or the domain that the user running the script is a member of (if they are the same, flip a coin), you can use a different method on the same class you're already trying to use:

For computer's domain:

[System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain()

For user's domain:

[System.DirectoryServices.ActiveDirectory.Domain]::GetUserDomain()

...yes it's in PowerShell but just invoke the either of the two methods above and they internally pass in the DirectoryContext relative to what you're looking for.

You might also get more information you're looking for by going up to the forest and retrieving that info as well:

[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
Sign up to request clarification or add additional context in comments.

4 Comments

I am interested in the metadata for the Domain (to which either a user or a computer can be part of). So getting details from the Forest wouldn't help.
Right, so the first two would be what you're after.
But how can I retrieve these values, SystemsContainer, LostAndFoundContainer, DomainControllersContainer?
Are you talking about the 'Domain Controllers' OU in the Domain partition (e.g. what you see in ADUC)? If so, either: [adsi]"LDAP://ou=Domain Controllers,dc=domain,dc=com" or Get-ADOrganizationalUnit "Domain Controllers"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.