Wednesday, December 29, 2010

Enable/Disable a Machine Account with C# in ASP.NET

I wrote a function to enable and disable machine accounts in Active Directory using LDAP functions in ASP.NET.  This is derivative of the earlier entry I had with the user account enable/disable. It uses a similar command but the filters/search criteria does indeed change. Please view below:


public void EnableDisableMachine(string server, 
        string name1, string obj1, Int32 sw)
    {


        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 = "(&(objectClass=*)(cn=" + obj1 + "))";




            searcher.PropertiesToLoad.Add("cn");
            searcher.PropertiesToLoad.Add("userPrincipalName");
            searcher.PropertiesToLoad.Add("sAMAccountName");
            searcher.PropertiesToLoad.Add("lockoutTime");


            if (sw == 0x200)
                Label1.Text = "Enabling Machine:<b> " + obj1 + "</b><br />";


            if (sw == 0x2)
                Label1.Text = "Disabling Machine:<b> " + obj1 + "</b><br />";


            Label1.Text += "------------------------------------------" +
            "---------------------------------- <br />";
            SearchResultCollection results = searcher.FindAll();
            foreach (SearchResult result in results)
            {
                string searchpath = result.Path;
                Console.WriteLine("path: {0}", searchpath);


                searcher.CacheResults = false;
                SearchResult result1 = searcher.FindOne();
                root = result1.GetDirectoryEntry();
                root.Properties["userAccountControl"].Value = sw;
                root.CommitChanges();




                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 />";


                


            }
            if (sw == 0x200)
                Label1.Text += "This machine has been enabled!!!"
                    + "<br />";


            if (sw == 0x2)
                Label1.Text += "This machine has been disabled!!!"
               + "<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...