Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1521
  • Last Modified:

i use dsquery to check groupmembership

I would like to check with a batch script if a user is member of a group

this should work with dsquery instead of ifmember

how can i use dsquery to make a simple if true then goto line xx query?

dsquery * domainroot -filter "(&(objectClass=group)(name=theGroup))" -l -d domain.local -attr member | find /c "Test UserA"

This will give me a value if there is any user in "theGroup"
Test UserA is in the Group one time so the result is 1
if i check for "Test User" it will give me the Result 4, because the text is part of Test UserA
Test UserB... so not a perfect solution any other ideas?
  • 2
1 Solution
Ony way is to check for the surrounding elements of the DN (the "cn=" at the beginning and the "," at the end:
dsquery * domainroot -filter "(&(objectClass=group)(name=theGroup))" -l -d domain.local -attr member | find /i /c "cn=Test UserA,"

Open in new window

Or do it the other way and query the user's group membership; this will work for nested groups as well:
dsquery user -name "Test UserA" | dsget user -memberof -expand | find /i /c "cn=theGroup,"

Open in new window

HelpdeskJBCAuthor Commented:
Ok and how can i use the return value to simply check with the if function?
can I save the result into a variable?
Sorry, should have mentioned that. "find" will return errorlevel 0 if the string was found, 1 otherwise.
dsquery user -name "Test UserA" | dsget user -memberof -expand | find /i /c "cn=theGroup,"
if %ErrorLevel%==0 goto NameFound
echo No group member
goto :eof
echo Group member

Open in new window


Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now