Link to home
Start Free TrialLog in
Avatar of k3eper
k3eper

asked on

Change Name then join Domain VBS

Ok this script changes the computer name to the service tag from bios (Dell machines) im trying to get it to join domain after but its not doing it. It completes with restart etc but it jsut doesnt seem to join domain. There are no errors that it reports. Anyone got any ideas? If i manually try join domain using the credentials in the script it works fine.
strComputer = "."
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", strSN
ws.RegWrite value2 & "NV Hostname", strSN
 
Const JOIN_DOMAIN             = 1
Const ACCT_CREATE             = 2
Const ACCT_DELETE             = 4
Const DOMAIN_JOIN_IF_JOINED   = 32
Const JOIN_UNSECURE           = 64
Const DEFERRED_SPN_SET        = 256
Const INSTALL_INVOCATION      = 262144
strDomain   = "DOMAINNAMEHERE"
strPassword = "PASSWORDHERE"
strUser     = "USERNAMEHERE"
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
Set objComputer = _
    GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
    strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" _
    & strComputer & "'")
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
    strPassword, _
    strDomain & "\" & strUser, _
    NULL, _
    JOIN_DOMAIN)
 
wshshell.run "shutdown /r" 
Set WSHShell = Nothing
WScript.Quit(0)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Pber
Pber
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
change

ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
    strPassword, _
    strDomain & "\" & strUser, _
    NULL, _
    JOIN_DOMAIN)

to

ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
   strPassword, strDomain & "\" & strUser, NULL, _
       JOIN_DOMAIN + ACCT_CREATE)
See this as well.  It's pretty much the exact method you are using, but at MSDN they too do the  JOIN_DOMAIN+ACCT_CREATE
http://msdn.microsoft.com/en-us/library/aa392154(VS.85).aspx 
Pber we are twins !!! :-)

sorry didn't refresh the page
(:
Avatar of k3eper
k3eper

ASKER

Great thanks works like a charm