How do I run a batch file to join domain?

575 pts.
Tags:
Active Directory
Active Directory Domain
AD
Batch files
Dear Expert, may i know how to run a batch file to join a domain? Thanks you.
ASKED: August 24, 2010  9:49 AM
UPDATED: August 30, 2010  9:21 AM

Answer Wiki

Thanks. We'll let you know when a new response is added.

Needs utilities netdom, dsquery, dsmove

Where OU_String is the fully qualified OU
Where DC1, DC2, DC3 are the elements of your domain, ie mycorp.example.com | DC1.DC2.DC3
Where JoinDomain is the domian to join. either full name or NetBIOS name (if NetBIOS used)
Where DMID is the ID for joining a workstation
Where DMPW is the password for the ID

This script uses dsquery to pull the distinguished / fully qualified name of the computer object. You could use dsquery to determine if the object exists as well. That adds a few lines and requires findstr to check for the computer name in the dsquery result as dsquery returns 0 so long as it was able to query the domain whether or not it finds any matches.

Note the case of ErrorLevel 2224 where the object already exists.

<pre>
cmd /c netdom join %COMPUTERNAME% /domain:%JoinDomain% /userd:%DMID% /passwordd:%DMPW% /ou:%OU_String%,DC=%DC1%,DC=%DC2%,DC=%DC3%
IF %ErrorLevel% EQU 0 (
ECHO ***%~n0 Returned ErrorLevel – %ErrorLevel% >>%Logfile%
ECHO ***%~n0 Successfully joined %Computername% to %JoinWorkGroup% >>%Logfile%
echo Successfully joined %Computername% to %JoinWorkGroup%
GOTO :CmpltJn
)
IF %ErrorLevel% EQU 2224 (
ECHO ***%~n0 Returned ErrorLevel – %ErrorLevel% >>%Logfile%
GOTO :TakeOvr
)
ECHO ***%~n0 Returned ErrorLevel – %ErrorLevel% >>%Logfile%
REM — Failed to join domain
GOTO :ErrorHandler

:TakeOvr
cmd /c netdom join %COMPUTERNAME% /domain:%JoinDomain% /userd:%DMID% /passwordd:%DMPW%
IF ErrorLevel 1 (
ECHO ***%~n0 Returned ErrorLevel – %ErrorLevel% >>%Logfile%
REM — Failed to join domain
GOTO :ErrorHandler
)
IF %ErrorLevel% EQU 0 ECHO ***%~n0 Returned ErrorLevel – %ErrorLevel% >>%Logfile%

REM — Moves object to specified OU. Ensures object is in correct location. Shorter than testing and then moving is simply moving.
cmd /c dsquery computer domainroot -d %JoinDomain% -name %ComputerName%|dsmove -newparent “%OU_String%,DC=%DC1%,DC=%DC2%,DC=%DC3%” -U %DMID% -P %DMPW%
IF ErrorLevel 1 (
ECHO ***%~n0 Returned ErrorLevel – %ErrorLevel% >>%Logfile%
REM — Failed to move to correct OU
GOTO :ErrorHandler
)
IF %ErrorLevel% EQU 0 ECHO ***%~n0 Returned ErrorLevel – %ErrorLevel% >>%Logfile%

:CmpltJn
</pre>

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Technochic
    What is it specifically that you want to join to the domain?
    57,010 pointsBadges:
    report
  • david2009
    Thank for your help, Pjb0222. I would like safe my time by running a batch file then i can join a domain for my PC in my company.
    575 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following