Quantcast
Channel: .NET – Kelly's Chronicles
Viewing all articles
Browse latest Browse all 48

Create Computer Account in Active Directory and add it to Domain with C#

$
0
0

I was asked to convert this code from my original post to C# by a friend who obviously didn’t want to do it himself. There was one conversion issue along the way I also fixed which is why I suspect he didn’t convert it himself. In other news, my assimilation into Memphis, TN is going well. I am slowly becoming accustomed to the heat and humidity and am trying to fight off the urge to eat everything in sight because the food is just amazing if a tad bit unhealthy. I finally am getting my car fixed and am looking forward to getting out and seeing this town for myself. Let’s hope it will be a lot of fun. Ok back to what you came here for. Here you go….. Smile

DirectoryEntry Entry = new DirectoryEntry(“LDAP://” + ActiveDirectoryTree.SelectedNode.Name, strDomain + “\\” + strUser, strPass, AuthenticationTypes.Secure);
            string ComputerName = null;
            string CompSystem = null;
            int ComputerCount = ComputerList.Items.Count;
            int i = 0;
            for (i = 0; i <= ComputerCount – 1; i++)
            {
                CompSystem = ComputerList.Items(i).Text;
                DirectoryEntry ComputerEntry = Entry.Children.Add(“CN=” + CompSystem + “”, “Computer”);
                ComputerEntry.Properties(“sAMAccountName”).Value = “” + CompSystem + “”;
                ComputerEntry.CommitChanges();
                int exp = Convert.ToInt32(ComputerEntry.Properties(“UserAccountControl”).Value);
                ComputerEntry.Properties(“UserAccountControl”).Value = exp | 0x1;
                ComputerEntry.CommitChanges();
                int val = Convert.ToInt32(ComputerEntry.Properties(“UserAccountControl”).Value);
                ComputerEntry.Properties(“UserAccountControl”).Value = val & ~0x2;
                ComputerEntry.CommitChanges();
                ComputersCreated.Text = ComputersCreated.Text + CompSystem + Environment.NewLine;
                ComputerEntry.Close();
                ComputerEntry.Dispose();
            }
            ComputersCreated.Text = ComputersCreated.Text + “——————————” + Environment.NewLine;
            Entry.Close();
            Entry.Dispose();

and to add to the domain:

string[] args = new string[5];
string[] args2 = new string[3];

// The one difference from the vb.net version. Commented this declaration since looping variables in ‘foreach’

//loops are declared in the ‘foreach’ header in C#:
//            ManagementObject comp = null;

            ManagementObjectCollection comps = null;
            ManagementClass clsComps = new ManagementClass(“Win32_ComputerSystem”);

            comps = clsComps.GetInstances();

            foreach (ManagementObject comp in comps)
            {
               // this == used to join the domain
               args[0] = “DOMAIN to join”;
               args[1] = “Password”;
               args[2] = “User with domain privs”;
               args[3] = “Specify OU Here (ou=test,dc=domain,dc=com)”;
               args[4] = “1”;
                UInt32 retVal = 0;

                retVal = comp.InvokeMethod(“JoinDomainOrWorkgroup”, args);

                retval2 = Convert.ToString(retVal);
                if (retval2 == “0”)
                {
                MessageBox.Show(“Welcome to the domain”);
                    Close();
                }
                else
                {

End If

             }


Join me on Facebook



Viewing all articles
Browse latest Browse all 48

Latest Images

Trending Articles





Latest Images