Thursday, December 23, 2010

Find All Locked Accounts in AD using C#

This script finds all locked accounts in Active Directory for a given domain. Please view below:



    public void SearchAllLockedObject(string server)
    {
        try
        {
            this.Label1.Style["text-align"] = "left";
            Label1.ForeColor = Color.Black;


            DirectoryEntry root1 = new DirectoryEntry(@"LDAP://rootdse");
            string path1 = root1.Invoke("GET", "defaultNamingContext").ToString();


            DirectoryEntry root = new DirectoryEntry(
               "LDAP://" + path1, txtUsername.Text, txtPassword.Text);
            //OU=Computers,OU=Home,dc=ds,dc=apollo,dc=com
            DirectorySearcher searcher = new DirectorySearcher(root);


            searcher.Filter = "(&(objectCategory=Person)(objectClass=User)(lockoutTime>=1))";
                searcher.PropertiesToLoad.Add("cn");
                searcher.PropertiesToLoad.Add("userPrincipalName");
                searcher.PropertiesToLoad.Add("sAMAccountName");
                searcher.PropertiesToLoad.Add("displayname");
                searcher.PropertiesToLoad.Add("description");
                searcher.PropertiesToLoad.Add("department");
                searcher.PropertiesToLoad.Add("manager");
                searcher.PropertiesToLoad.Add("homephone");
                searcher.PropertiesToLoad.Add("mobile");
                searcher.PropertiesToLoad.Add("location");
                searcher.PropertiesToLoad.Add("streetaddress");
                searcher.PropertiesToLoad.Add("mail");
                searcher.PropertiesToLoad.Add("lockoutTime");
                searcher.PropertiesToLoad.Add("telephonenumber");
            


            Label1.Text = "";
            Label1.Text += "------------------------------------------" +
                "---------------------------------- <br />";
            SearchResultCollection results = searcher.FindAll();
            foreach (SearchResult result in results)
            {
                string searchpath = result.Path;
                Console.WriteLine("path: {0}", searchpath);
              
                ResultPropertyCollection rpc = result.Properties;
                foreach (string property in rpc.PropertyNames)
                {
                    foreach (object value in rpc[property])
                    {
                        if (property == "mail")
                        {
                            Label1.Text += "<b>" + property +
                                "</b>: <a href='mailto:" + value + "'>" + value + "</a><br />";
                        }
                        else
                        {
                            Label1.Text += "<b>" + property +
                                "</b>: " + value + "<br />";
                        }
                    }
                }
                Label1.Text += "---------------------------------------"
                + "------------------------------------- <br />";
            }
        }
        catch (Exception ex)
        {
            Label1.Text += ex.Message;


        }
    }

No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...