Solved

Got COM ERROR when retrieving user members from a group.

Posted on 2014-12-18
3
183 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
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…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

910 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

22 Experts available now in Live!

Get 1:1 Help Now