How to join a workgroup computer to a domain remotely

VoidnessMD picture VoidnessMD · Aug 11, 2012 · Viewed 10.9k times · Source

Scenario: I'm using ComputerA in a domain with admin credentials. Remote ComputerB (with known admin credentials) is in a workgroup. ComputerB needs to join a specific domain (Different than ComputerA's) in a specified OU which my active ComputerA credentials have the rights to do. I Cannot use an external program such as NETDOM and would prefer to use VBScript.

Any help is much appreciated!

Answer

peter picture peter · Aug 12, 2012

Try this, i adapted a piece of script from http://www.tek-tips.com/viewthread.cfm?qid=1240726

strComputer = "ComputerB"
strPassword = "mypassword"
strDomain   = "mydomain"
strUser     = "myusername"

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

ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strDomain & "\" & strUser, "ou=My_Computer_OU,DC=mycorp,dc=com, _
        JOIN_DOMAIN + ACCT_CREATE)

If ReturnValue = 0 Then
    MsgBox "Computer added to domain under old name without error. proceeding to change computer name. "
Else
    MsgBox "Computer not added to domain successfully. Return value: " & ReturnValue
End If