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?

[214 byte] By [Lunatic_Magnet] at [2007-12-28]
# 1

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

JoshuaMorgan at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
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 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

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

JoshuaMorgan at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...