Good Morning! Last night I watched a movie called “A Righteous Kill” with Robert De Niro and Al Pacino. With actors like these I knew it was going to be a treat but they still exceeded my expectations. Without spoiling it for you, I will tell you that the ending is not what you will even remotely see coming.
So today’s topic is you want to add a computer account to Active Directory and add it to a domain with vb.net. Adding it to the domain uses WMI and is a different code sample. I wrote this years ago when we had a bunch of people we were adding at once and didn’t want to do each one individually. Make it a great day!
Adding to Active Directory:
Dim Entry As New DirectoryEntry("LDAP://" & ActiveDirectoryTree.SelectedNode.Name, strDomain & "\" & strUser, strPass, AuthenticationTypes.Secure)
Dim ComputerName As String = Nothing
Dim CompSystem As String = Nothing
Dim ComputerCount As Integer = ComputerList.Items.Count
Dim i As Integer = 0
For i = 0 To ComputerCount – 1
CompSystem = ComputerList.Items(i).Text
Dim ComputerEntry As DirectoryEntry = Entry.Children.Add("CN=" & CompSystem & "", "Computer")
ComputerEntry.Properties("sAMAccountName").Value = "" & CompSystem & ""
Dim exp As Integer = CInt(ComputerEntry.Properties("UserAccountControl").Value)
ComputerEntry.Properties("UserAccountControl").Value = exp Or &H1
Dim val As Integer = CInt(ComputerEntry.Properties("UserAccountControl").Value)
ComputerEntry.Properties("UserAccountControl").Value = val And Not &H2
ComputersCreated.Text = ComputersCreated.Text & CompSystem & vbCrLf
ComputersCreated.Text = ComputersCreated.Text & "——————————" & vbCrLf
Add to Domain:
Dim args(4) As String
Dim args2(2) As String
Dim comp As ManagementObject
Dim comps As ManagementObjectCollection
Dim clsComps As New ManagementClass("Win32_ComputerSystem")
comps = clsComps.GetInstances()
For Each comp In comps
‘this is 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"
Dim retVal As UInt32
retVal = comp.InvokeMethod("JoinDomainOrWorkgroup", args)
retval2 = Convert.ToString(retVal)
If retval2 = "0" Then
MsgBox("Welcome to the domain")