Solved

Got COM ERROR when retrieving user members from a group.

Posted on 2014-12-18
3
180 Views
Last Modified: 2014-12-19
Hi,
I'm using vs2012, C#.
I'm bind to AD object class=group.  It contains members that are AD accounts of objectclass=user.
I used the code I found on MSDN, http://msdn.microsoft.com/en-us/library/ms180906(v=vs.90).aspx,  but it wont' compile so I added either Object or UserPricipal in the IEnumerable part of the code and both gets me the error below.  What class type do I use for the user object for IEnumerable?

DirectoryEntry group = new DirectoryEntry("LDAP://CN=Sales,DC=Fabrikam,DC=COM");
object members = group.Invoke("Members", null);
foreach(object member in (IEnumerable<object>)members)
{
    DirectoryEntry x = new DirectoryEntry(member);
    Console.WriteLine(x.Name);
}

"System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'System.Collections.Generic.IEnumerable`1[System.Object]'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{AEF9EC8A-1E73-365B-8DA2-800A3A6166E6}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).\r\n   at

I change the code to

foreach (object groupMember in group.Properties["member"])

then it breaks at the next line at

DirectoryEntry member = new DirectoryEntry(groupMember);

And now I get error


-            $exception      {"The value provided for adsObject does not implement IADs."}      System.Exception {System.ArgumentException}
0
Comment
Question by:lapucca
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
ID: 40508460
Try using PrincipalContext

PrincipalContext AD = new PrincipalContext(ContextType.Domain, Sales);
UserPrincipal u = new UserPrincipal(AD);
PrincipalSearcher search = new PrincipalSearcher(u);
	
Foreach (UserPrincipal result in search.FindAll())
{
    Console.WriteLine(result.Name);
}

Open in new window


Here are some methods that have worked for me previously and exports all users to a file in csv format
      void GetAllADUSersForDomain(string outputFile, string domain)
      {
         PrincipalContext AD = new PrincipalContext(ContextType.Domain, domain);
         UserPrincipal u = new UserPrincipal(AD);
         PrincipalSearcher search = new PrincipalSearcher(u);

         using (StreamWriter sw = new StreamWriter(outputFile, true))
         {
            sw.WriteLine("Domain,User Name,Staff Number,Email,OU");

            foreach (UserPrincipal result in search.FindAll())
            {
               if (result.DisplayName != null)
               {
                  sw.WriteLine(string.Concat(domain, ",", 
                               result.SamAccountName, ",", 
                               result.EmployeeId, ",", 
                               result.EmailAddress, ",", 
                               GetOU(result.DistinguishedName)));
                  
               }
            }
         }
      }

      private static string GetOU(string distinguishedName)
      {
         string[] arr = distinguishedName.Split(',');
         for (int i = 0; i < arr.Length; i++)
         {
            if (arr[i].Substring(0, 2) == "OU")
            {
               return arr[i].Substring(3);
            }
         }
         return null;
      }

Open in new window

0
 

Author Comment

by:lapucca
ID: 40509558
I was thinking about putting all user returned as a DirectoryEntry objct and store in a general colleciton, List<DirectoryEntry>  I want to bind that to a gridview control showing about 6 properties and allow users to change them.  Do you think that's a good way to do it or even doable?

In your fist code example, this code returns all users in the domain, I think.  I would like to retrieve all members (all their user information as an object so can be updated by user in my app) in a Security group only.  I can bind to that Security Group(or user group), I can see looping through it showing all the users members in it, cn=xxxx, ou=xxx, DC=xxx, Dc=xxx...
How can I put these users in a collection, bind it to a gridview?

Thank you.
0
 

Author Comment

by:lapucca
ID: 40509840
Okay, I used the properties method and what's returned is the DN of the user object so I create new DirectoryEntry object using that to obtain the user object.  Thank you for your help.  
My next challenge is how to allow user editing and writing back to AD.  maybe you can help too.
Thank you.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now