Search code examples
c#.netactive-directoryquery-by-example

Is it possible to search for a Guid in a "Query By Example"?


I need to find whether a computer with a given Guid exists inside a given OU.

To do this, I'd prefer to write a Query By Example that searches for a computer matching a Guid. For example:

PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, container);
ComputerPrincipal computer = new ComputerPrincipal(context);

computer.Guid = guidToMatch;

PrincipalSearcher searcher = new PrincipalSearcher(computer);
// Get the computer if it exists...

Of course this doesn't work, because the ComputerPrincipal.Guid field is read-only. Furthermore, the ComputerPrincipal.AdvancedSearchFilter does not contain a Guid field.

Is this possible, or is there some reason I wouldn't want to do this anyway (like a better alternative)?


Solution

  • Looks like the way to handle this is to use FindByIdentity():

    PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, container);
    ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(context, guidToMatch);