Joining an AD domain with VB2005
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?
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?
Add a reference to System.Management and Import it to your class
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
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