Solved

Got COM ERROR when retrieving user members from a group.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Most of the applications these days are on Cloud. Cloud is ubiquitous with many service providers in the market. Since it has many benefits such as cost reduction, software updates, remote access, disaster recovery and much more.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

785 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