# [SOLVED] VB Script To Join A Computer To A Domain



## Mattman86

I am trying to create a VB Script for my work that we can run on our 3000+ computers to join them to the domain. I am running into an error on line 30 and since i am pretty new to VB, I have no idea what the problem is. Here is my code so far. Any help would be greatly appreciated.



Code:


' JoinDomain.vbs
' VBScript program to join a computer to a domain.
' The computer account is created in Active Directory.
' The computer must have XP or above.
' The AD must be W2k3 or above.

Dim strDomain, strUser, strPassword
Dim objNetwork, strComputer, objComputer, lngReturnValue
Dim strOU

Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Const ACCT_DELETE = 4
Const WIN9X_UPGRADE = 16
Const DOMAIN_JOIN_IF_JOINED = 32
Const JOIN_UNSECURE = 64
Const MACHINE_PASSWORD_PASSED = 128
Const DEFERRED_SPN_SET = 256
Const INSTALL_INVOCATION = 262144

strDomain = InputBox("Enter the NetBIOS name of the domain")
strUser = InputBox("Enter administrator name")
strPassword = InputBox("Enter password")
strOU = InputBox("Enter the DN of the OU where computer account created")

Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName

Set objComputer = GetObject("winmgmts:" _
& "{impersonationLevel=Impersonate,authenticationLev el=Pkt}!\\" & _
strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
strComputer & "'")

lngReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
strPassword, strDomain & "\" & strUser, strOU, _
JOIN_DOMAIN + ACCT_CREATE)

Wscript.Echo "ReturnValue = " & CStr(lngReturnValue)


----------



## TheOutcaste

*Re: VB Script To Join A Computer To A Domain*

Line 30 has this:
*authenticationLev el=Pkt*

Should be this:
*authenticationLevel=Pkt*

Without knowing what the error is hard to say if that's it, or if the space slipped in when you posted the script, but that caught my eye.

Pretty new to VB Script myself so don't know if I can be much help.


----------



## Mattman86

*Re: VB Script To Join A Computer To A Domain*

Oh, thanks for pointing that out. I changed it and the program runs through but it doesn't seem to work. It returns the value 2691. Any idea what that could mean?


----------



## Mattman86

*Re: VB Script To Join A Computer To A Domain*

So i found at another forum that 2691 means that the computer is already on a domain so i have to add:



Code:


Const NETSETUP_ACCT_DELETE = 2 'Disables computer account in domain.
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName

Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
 strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
strDomain1 = objComputer.Domain
intReturn = objComputer.UnjoinDomainOrWorkgroup _
 (strPassword, strDomain1 & "\" & strUser, NETSETUP_ACCT_DELETE)

I did that and now i get an error code of 87.

Here is my entire code again:



Code:


' JoinDomain.vbs
' VBScript program to join a computer to a domain.
' The computer account is created in Active Directory.
' The computer must have XP or above.
' The AD must be W2k3 or above.

Dim strDomain, strUser, strPassword
Dim objNetwork, strComputer, objComputer, lngReturnValue
Dim strOU

Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Const ACCT_DELETE = 4
Const WIN9X_UPGRADE = 16
Const DOMAIN_JOIN_IF_JOINED = 32
Const JOIN_UNSECURE = 64
Const MACHINE_PASSWORD_PASSED = 128
Const DEFERRED_SPN_SET = 256
Const INSTALL_INVOCATION = 262144

'delete existing account
Const NETSETUP_ACCT_DELETE = 2 'Disables computer account in domain.
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName

Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
 strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
strDomain1 = objComputer.Domain
intReturn = objComputer.UnjoinDomainOrWorkgroup _
 (strPassword, strDomain1 & "\" & strUser, NETSETUP_ACCT_DELETE)

'add new account on new domain
strDomain = InputBox("Enter the NetBIOS name of the domain")
strUser = InputBox("Enter administrator name")
strPassword = InputBox("Enter password")
strOU = InputBox("Enter the DN of the OU where computer account created")

Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName

Set objComputer = GetObject("winmgmts:" _
& "{impersonationLevel=Impersonate,authenticationLevel=Pkt}!\\" & _
strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
strComputer & "'")

lngReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
strPassword, strDomain & "\" & strUser, strOU, _
JOIN_DOMAIN + ACCT_CREATE)

Wscript.Echo "ReturnValue = " & CStr(lngReturnValue)


----------



## TheOutcaste

*Re: VB Script To Join A Computer To A Domain*

Seems to me this value is designed just for the situation of joining a domain if a PC is already a member of a domain:
Const DOMAIN_JOIN_IF_JOINED = 32
From http://msdn.microsoft.com/en-us/library/aa392154(VS.85).aspx


> 32 (0x20) - Allows a join to a new domain, even if the computer is already joined to a domain.


Same page only shows two return values, 0 and 0x80041087, don't know if that last translates to the 87 you are seeing.
Says you need to use AuthenticationLevel=PktPrivacy instead of just Pkt, so might give that a try.
87 from the list of Win32 Error codes is ERROR_INVALID_PARAMETER, so there may be a problem with one of the parameters. Perhaps the strOU value



> _AccountOU_ [in, optional]
> 
> Specifies the pointer to a constant null-terminated character string that contains the RFC 1779 format name of the organizational unit (OU) for the computer account. If you specify this parameter, the string *must contain a full path*, otherwise _Accent_ must be NULL.
> 
> Example: "OU=testOU, DC=domain, DC=Domain, DC=com"


Looks like that might be the one most likely to be mistyped.
(I think _Accent_ is a typo in the article, should say _AccountOU_)


----------



## Mattman86

*Re: VB Script To Join A Computer To A Domain*

Well, my code works great if the computer isn't already on another domain. So, I just made a script to put it on one and you have to manually take it of the other one before you start.


----------

