Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > Joining an AD domain with VB2005
 

Joining an AD domain with VB2005

Hi

I would like to create a little application using VB2005 to join a computer to an AD domain. I know there are many vbscripts to do this but I'm hoping to make a single gui app to achieve this.

Any thoughts?


Lunatic_Magnet  Saturday, January 13, 2007 7:54 PM

Add a reference to System.Management and Import it to your class

then add this code

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 unjoin the domain
''args2(0) = "Password"
''args2(1) = "User with privs"
''args2(2) = "0"
''comp.InvokeMethod("UnjoinDomainOrWorkgroup", args2)

'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"

comp.InvokeMethod("JoinDomainOrWorkgroup", args)
Next

Reference: http://msdn2.microsoft.com/en-us/library/aa392154.aspx

Joshua Morgan  Sunday, January 14, 2007 8:24 PM

Add a reference to System.Management and Import it to your class

then add this code

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 unjoin the domain
''args2(0) = "Password"
''args2(1) = "User with privs"
''args2(2) = "0"
''comp.InvokeMethod("UnjoinDomainOrWorkgroup", args2)

'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"

comp.InvokeMethod("JoinDomainOrWorkgroup", args)
Next

Reference: http://msdn2.microsoft.com/en-us/library/aa392154.aspx

Joshua Morgan  Sunday, January 14, 2007 8:24 PM
Great thanks for the reply. Is it possible to have some error checking i.e "Computer Account already exists" etc The code appears to run correctly but I get no feedback on the outcome of it..
Lunatic_Magnet  Monday, January 15, 2007 3:05 PM

You can do something like this, though I have not tested this code, but you get the idea:

Private Function IsMemberOfDomain(ByVal ComputerName As String) As Boolean

Dim entry As DirectoryEntry = New DirectoryEntry("LDAP://yourdomain")

Dim DirSearcher As DirectorySearcher = New DirectorySearcher(entry)

DirSearcher.Filter = "(objectClass=computer)"

For Each result As SearchResult In DirSearcher.FindAll

If result.GetDirectoryEntry.Name.ToString = ComputerName Then

Return True

End If

Next

Return False

End Function

Joshua Morgan  Saturday, January 20, 2007 7:08 PM

You can use google to search for other answers

Custom Search

More Threads

• how do you open the help and support of winxp and vista?
• VB App freezes when accessing properties of Excel object...
• Calendar Control
• How to use the command print...
• Text file to database
• Reading data into Visual Studio (VB)
• PasswordBox Help
• VBA throwing a run-time error 429 "ActiveX component can't create object"
• Calculate Tif images in a specific folder
• system.drawing.fontstyle