VBS Join domain script?

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
Hey guys,

So I am making a script that joins a system to the domain and names it based on the service tag. Every part works flawlessly except the joining to the domain part. Im hoping someone can take a look at the code and help me understand why it isnt working:

Code:
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2

Dim strComputer, strSN, oWMI, oCmp, oOS, sReturn, sUser, sPassword, sDomain, sOU

sUser = "<USERNAME>"
sPassword = "<PASSWORD>"
sDomain = "<DOMAIN>"
strComputer = "."

Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
For Each oCmp in oWMI.InstancesOf("Win32_ComputerSystem")
sReturn = oCmp.JoinDomainOrWorkgroup(sDomain, sPassword, _
sDomain & "\" & sUser, sOU, JOIN_DOMAIN)

next

'wScript.sleep 20000
'Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
'Set colSMBIOS = objWMIService.ExecQuery ("Select * from Win32_SystemEnclosure")
'For Each objSMBIOS in colSMBIOS
 ' strSN = objSMBIOS.SerialNumber
  'If strSN <> "" Then exit For
'Next
 
'Set ws = WScript.CreateObject("WScript.Shell")
'value1 = "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\"
'value2 = "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\ParameterS\"
'ws.RegWrite value1 & "ComputerName", "WN7X64-" + strSN
'ws.RegWrite value2 & "NV Hostname", "WN7X64-" + strSN 

'wScript.sleep 2000
'Dim oShell

'Set oShell = CreateObject("Wscript.Shell")
'sReturn = oShell.Run("%comspec% /c shutdown -r -t 0 -f")
I commented off the parts that are working (timers, name the computer, shut down after it runs etc..).

Basically the code runs through (verified with a message box) and just doesnt do anything, it wont add it to the domain (yes I am a domain admin).
 

Tweak155

Lifer
Sep 23, 2003
11,449
264
126
The examples at MS say to pass in JOIN_DOMAIN + ACCT_CREATE as the last parameter.

My only other thought is sOU has no definition. You may be intending to pass a NULL, but I'm not sure VBS defaults to NULL without specifying.
 

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
The examples at MS say to pass in JOIN_DOMAIN + ACCT_CREATE as the last parameter.

My only other thought is sOU has no definition. You may be intending to pass a NULL, but I'm not sure VBS defaults to NULL without specifying.

Good ideas, I dont get any errors though when running the script.

I modified the code to:
Code:
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2

Dim strComputer, strSN, oWMI, oCmp, oOS, sReturn, sUser, sPassword, sDomain

sUser = "<USERNAME>"
sPassword = "<PASSWORD>"
sDomain = "<DOMAIN>"
strComputer = "."

Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
For Each oCmp in oWMI.InstancesOf("Win32_ComputerSystem")
sReturn = oCmp.JoinDomainOrWorkgroup(sDomain, sPassword, _
sDomain & "\" & sUser, NULL, JOIN_DOMAIN + ACCT_CREATE)

next
and it yields the same results

Im not too familiar with how domains and stuff like that works, so is it possible that this is an issue on that end?

FAIL edit - I just checked the code again and realized that I forgot to enter my credentials after I made the code change :p

Thanks to you the script is now working perfectly!
 
Last edited:

Tweak155

Lifer
Sep 23, 2003
11,449
264
126
Good ideas, I dont get any errors though when running the script.

I modified the code to:
Code:
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2

Dim strComputer, strSN, oWMI, oCmp, oOS, sReturn, sUser, sPassword, sDomain

sUser = "<USERNAME>"
sPassword = "<PASSWORD>"
sDomain = "<DOMAIN>"
strComputer = "."

Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
For Each oCmp in oWMI.InstancesOf("Win32_ComputerSystem")
sReturn = oCmp.JoinDomainOrWorkgroup(sDomain, sPassword, _
sDomain & "\" & sUser, NULL, JOIN_DOMAIN + ACCT_CREATE)

next
and it yields the same results

Im not too familiar with how domains and stuff like that works, so is it possible that this is an issue on that end?

FAIL edit - I just checked the code again and realized that I forgot to enter my credentials after I made the code change :p

Thanks to you the script is now working perfectly!

Awesome glad to hear :thumbsup: