Is it possible to search for a Guid in a “Query By Example”?
-
18-03-2021 - |
Question
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);
OTHER TIPS
Another way to handle this is to do a base search of the form . This will essentially allow you to search for the object by objectGUID and get back the match, be it a computer or some other type of object. You could then inspect the object and see if it is what you had in mind...