Create Computer Account in Active Directory and add it to Domain with vb.net

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!


Join me on Facebook

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 & ""
                ComputerEntry.CommitChanges()
                Dim exp As Integer = CInt(ComputerEntry.Properties("UserAccountControl").Value)
                ComputerEntry.Properties("UserAccountControl").Value = exp Or &H1
                ComputerEntry.CommitChanges()
                Dim val As Integer = CInt(ComputerEntry.Properties("UserAccountControl").Value)
                ComputerEntry.Properties("UserAccountControl").Value = val And Not &H2
                ComputerEntry.CommitChanges()
                ComputersCreated.Text = ComputersCreated.Text & CompSystem & vbCrLf
                ComputerEntry.Close()
                ComputerEntry.Dispose()
            Next
            ComputersCreated.Text = ComputersCreated.Text & "——————————" & vbCrLf
            Entry.Close()
            Entry.Dispose()

 

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")
                    Close()
                Else

End If

Advertisements
  1. #1 by Jennifer on March 3, 2009 - 9:08 am

    Agreed on the movie, I thought it was very well done.Keep up the good info on the coding, you never know when you\’ll need it.

  1. Create Computer Account in Active Directory and add it to Domain with C# | Kelly's Chronicles

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: