Share via


Get the list of Users from Security group in active directory using SQL

Question

Friday, December 19, 2014 1:08 AM

I have  a table with list of members of a security group. I need to get the list of members from Security Group using SQL so I can update the table everyday. How can this be done?

All replies (1)

Friday, December 19, 2014 8:58 AM âś…Answered | 1 vote

Nothing to do with wpf then.

.

You realise you can use a group for security purposes rather than a list of users?

.

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");

// if found....
if (group != null)
{
   // iterate over members
   foreach (Principal p in group.GetMembers())
   {
      Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);

      // do whatever you need to do to those members
      UserPrincipal theUser = p as UserPrincipal;

      if(theUser != null)
      {
          if(theUser.IsAccountLockedOut()) 
          {
               ...
          }
          else
          {
               ...
          }
      }
   }
}

http://stackoverflow.com/questions/7242226/get-members-of-active-directory-group-and-check-if-they-are-enabled-or-disabled

or adapt this

http://www.codeproject.com/Articles/22800/Enumerating-Users-using-WMI-NET-and-C

If this needs to be run as a daily batch job you can create a script runs an exe.

.

You can alternatively use openrowset or openquery from sql server if you have it configured right:

http://www.skylinetechnologies.com/Blog/Article/1309/Querying-Active-Directory-through-SQL-Server-Using-OpenRowset-and-OpenQuery.aspx

Please don't forget to upvote posts which you like and mark those which answer your question.
My latest Technet article - Dynamic XAML