Neil Hobson
Neil Hobson

Reputation: 515

How can I get a list of Active Directory Users (Only the Users that appear in the windows Logon Screen)

How can I get a list of Active Directory Users (Only the Users that appear in the windows Logon Screen)

I have tried many methods using Windows Principle library & WMI Select commands. I keep getting Admministrator, Guest & some other VUSRNEIL-DELL. Neither of these 3 user accounts appear on the Logon screen. How can I Deifferientiate between these user types?

Upvotes: 0

Views: 4499

Answers (2)

MacG
MacG

Reputation: 271

Check Win32_LogonSession and Win32_LoggedOnUser classes (where Win32_LogonSession.LogonType='2') for the current logged on user that you can then relate back to the Win32_Account class ;)

Upvotes: 0

Hassan Boutougha
Hassan Boutougha

Reputation: 3919

//Add a reference on System.DirectoryServices.dll
    using System.DirectoryServices;    
    //Connect to your LDAP
    DirectoryEntry Ldap = new DirectoryEntry("LDAP://ADName", "Login", "Password");
    DirectorySearcher searcher = new DirectorySearcher(Ldap);
    //specify that you search user only by filtering AD objects
    searcher.Filter = "(objectClass=user)";
    //Loop on each users
     foreach( SearchResult result in searcher.FindAll() )
        {
           // On récupère l'entrée trouvée lors de la recherche
           DirectoryEntry DirEntry = result.GetDirectoryEntry();

           //On peut maintenant afficher les informations désirées
           Console.WriteLine("Login : " + DirEntry.Properties["SAMAccountName"].Value);
           Console.WriteLine("FirstName: " + DirEntry.Properties["sn"].Value);
           Console.WriteLine("LastName: " + DirEntry.Properties["givenName"].Value);
           Console.WriteLine("Email : " + DirEntry.Properties["mail"].Value);
           Console.WriteLine("Phone: " + DirEntry.Properties["TelephoneNumber"].Value);
           Console.WriteLine("Description : " + DirEntry.Properties["description"].Value);

           Console.WriteLine("-------------------");
        }

Upvotes: 1

Related Questions