Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Join Domain with VB.NET 1

Status
Not open for further replies.

Mootser13

Programmer
Sep 7, 2005
30
US
I want to write a program that joins a pc to my domain and then move the PC to a specific OU.
I have read the MSDN article about using the following

Public Declare Function NetJoinDomain Lib "NETAPI32.dll" _
(ByVal lpServer As String, _
ByVal lpDomain As String, _
ByVal lpAccountOU As String, _
ByVal lpAccount As String, _
ByVal lpPassword As String, _
ByVal fJoinOptions As Long) As Long

then :

RetVal = NetJoinDomain(vbNullChar, _
strDOMAIN, _
strOFCELOC, _
strACCOUNT, _
strPASSWORD, _
NETSETUP_JOIN_DOMAIN Or NETSETUP_DOMAIN_JOIN_IF_JOINED Or NETSETUP_ACCT_CREATE)
I keep getting weird "retval" like 9885984895498.... and so on.
Is there another way I can do this? I know netdom can create a PC account in the OU, but not actually make the pc join the domain.
I have done this before successfully in vb 6.0, so I can't understand why this doesn't work.
Help...
Mootser13
 
Add a reference to System.Management and Import it to your class:

then add this code:

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

For a reference on the arguments, see
 
How can I get a return value is it is successfull or not ?
 
Did you read the article that I pointed you to? There is a section called Return Value toward the bottom. For success it will be 0.

It would look somthing like:

Code:
Dim retVal as Long

retVal = comp.InvokeMethod("JoinDomainOrWorkgroup", args)
If retVal = 0 Then
    'Domain was successfully joined
Else
    'Something went wrong
End If

Note that you will need to reboot the computer after you do this for it to take effect.
 
Yeah, I was not Dim'ing the retval as long, but as uint32.
Thanks
 
Actually I get an error "Cast from type 'Uint32' to type 'long' is not valid"
Hmmm, wonder if I am doing something wrong
 
Ok, I have fixed the Uint32 error and I keep getting 1332 error return message.
ERROR_NONE_MAPPED
1332 No mapping between account names and security IDs was done.

But when I create an account using Active Directory Users and Computers then it works perfectly, but I want my program to create the account.

Maybe the "1" for accout_create should not be a string but a Uint32 ??
 
I was finally able to get this to work properly.

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.com"
args(1) = strPASSWORD 'from box 1
args(2) = strACCOUNT ' from box 2
args(3) = strOFCELOC.ToString 'from dropdwn
args(4) = 1 Or 2 'join or create account
Dim retVal As UInt32
retVal = comp.InvokeMethod("JoinDomainOrWorkgroup", args)
retval2 = Convert.ToString(retVal)
If retval2 = "0" Then
MsgBox("Welcome to the domain")
Close()
Else
LblStatus.Text = Convert.ToString(retVal) & " Error occurred while joining the domain"
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top