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>
What is it specifically that you want to join to the domain?
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.