Automated batch script using netdom to join domain

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bardman
Posts: 10
Joined: 08 Dec 2011 23:52

Automated batch script using netdom to join domain

#1 Post by bardman » 09 Dec 2011 00:24

Though, I look like one of the most intelligent human beings to ever walk the face of this earth, I am not proficient in scripting nor programming.

My basic current setup is:
Windows 2003 soon to be 2008 R2 AD schema
XP SP3 workstations soon to be Windows 7 (like in a year or two). This is the image I am trying to fully automate the domain join procedure detailed in this post.

I use several batch scripts and a couple of vbs files to run after the image is SysPrep'ed using GhostCast server or the Ghost Console of GSS version 2.5. Basically, WSNAME renames the computers via the mac address from a text file and reboots.
A program has to update its SQL DB instance after the computer is renamed, then the computer has to reboot again.
After this reboot I would like for this script to perform the following ALL AUTOMATED:

A batch script using netdom is preferred.

1. Queries the computer name via string or whatever is appropriate
2. If computer name matches string, join this computer to this domain and OU then reboot and script should end or
3. If computer name does not match, goto to next statement to check for match and join this computer to this domain and OU then reboot and script should end or
4. If no string matches are found, recurse through statements and join computer to the default OU then reboot and script should end

In essence, all computers will join the domain under a specified container.

The important stuff has been changed to protect the innocent!

This attempt was pooled from several websites but primarily from http://stackoverflow.com/questions/5491383/find-out-whether-an-environment-variable-contains-a-substring. If this can be better done using vbs or any other programs, please provide the files. I also understand that the single percent sign (%) may have to be a double percent sign (%%) when being ran as a batch script.

I have about four other file iterations in this attempt to get this thing to work as well. So I have been trying in vain for the past couple of weeks.

The main issue seems to be getting the right syntax to query the computername based on the findstr command. I can manipulate the statements to get them to work manually via the command line but not as one batch script.

After more research, it also seems that possibly using the Set command to set variables for the search strings may also be plausible. Some of these computers belong in the same OU so a couple of these statements can be revised or omitted as long as the proper query string is accounted.

Please take and revise it or even trash it altogether. I just want something that is going to work flawlessly. Any and all help is greatly appreciated.

THIS SCRIPT MANUALLY WORKS but obviously it is only interactive.
::netdom join %computername% /Domain:handsome.and.smart.com /userd:delegate /passwordd:topsecret /OU:"ou=Secret Stations,ou=Domain Computers,dc=handsome,dc=and,dc=smart,dc=com" /REBOOT

Code: Select all

echo.%computername%|findstr /B /I /C:"12" >nul 2>&1 && goto move_to_XPOU || goto move_to_secretOU
echo.%computername%|findstr /B /I /C:"745" >nul 2>&1 && goto move_to_secretOU || goto move_to_85OU
echo.%computername%|findstr /B /I /C:"85-712" >nul 2>&1 && goto move_to_85OU || goto move_to_859OU
echo.%computername%|findstr /B /I /C:"85-9" >nul 2>&1 && goto move_to_859OU || goto move_to_ctdOU
echo.%computername%|findstr /B /I /C:"CTD" >nul 2>&1 && goto move_to_ctdOU || goto move_to_coajOU
echo.%computername%|findstr /B /I /C:"COAJ" >nul 2>&1 && goto move_to_coajOU || goto move_to_65OU
echo.%computername%|findstr /B /I /C:"65" >nul 2>&1 && goto move_to_65OU || goto move_to_16
echo.%computername%|findstr /B /I /C:"16" >nul 2>&1 && goto move_to_16 || goto move_to_104OU
echo.%computername%|findstr /B /I /C:"104" >nul 2>&1 && goto move_to_104OU || goto move_to_secretOU
echo.%computername%|findstr /B /I /C:"Sec" >nul 2>&1 && goto move_to_secretOU || goto move_to_scscOU
echo.%computername%|findstr /B /I /C:"SCSC" >nul 2>&1 && goto move_to_scscOU || goto move_to_defaultOU
echo.%computername%|findstr /B /I /C:"740" >nul 2>&1 && goto move_to_defaultOU || goto end

::If name does not match any strings, place computer in default container.
echo.%computername%|findstr /B /I /C:"" >nul 2>&1 && goto move_to_default_OU || goto end



:move_to_XPOU
echo.%computername%|findstr /B /I /C:"12" >nul 2>&1 && netdom JOIN %computername% /Domain:handsome.and.smart.com /userd:delegate /passwordd:topsecret /OU:"ou=XP Client,ou=Domain Computers,dc=handsome,dc=and,dc=smart,dc=com" /REBOOT && goto end || goto move_to_ghostOU


:move_to_secretOU
echo.%computername%|findstr /B /I /C:"745" >nul 2>&1 && netdom JOIN %computername% /Domain:handsome.and.smart.com userd:delegate /passwordd:topsecret /OU:"ou=Secret Stations,ou=Domain Computers,dc=handsome,dc=and,dc=smart,dc=com" /REBOOT:34 && goto end || goto move_to_puka


:move_to_85OU
echo.%computername%|findstr /B /I /C:"85-712" >nul 2>&1 && netdom JOIN %computername% /Domain:handsome.and.smart.com /userd:delegate /passwordd:topsecret /OU:"ou=85 COMPUTERS,ou=Domain Computers,dc=handsome,dc=and,dc=smart,dc=com" /REBOOT && goto end || goto move_to_859OU


:move_to_859OU
echo.%computername%|findstr /B /I /C:"85-9" >nul 2>&1 && netdom JOIN %computername% /Domain:handsome.and.smart.com /userd:delegate /passwordd:topsecret /OU:"OU=859 CLIENTS,OU=BLDG 85 OU,dc=handsome,dc=and,dc=smart,dc=com" /REBOOT && goto end || goto move_to_ctdOU 


:move_to_ctdOU
echo.%computername%|findstr /B /I /C:"CTD" >nul 2>&1 && netdom JOIN %computername% /Domain:handsome.and.smart.com /userd:delegate /passwordd:topsecret /OU:"ou=CTD COMPUTERS,ou=Domain Computers,dc=handsome,dc=and,dc=smart,dc=com" /REBOOT && goto end || goto move_to_coajOU


:move_to_coajOU
echo.%computername%|findstr /B /I /C:"COAJ" >nul 2>&1 && netdom JOIN %computername% /Domain:handsome.and.smart.com /userd:delegate /passwordd:topsecret /OU:"ou=COAJ COMPUTERS,ou=Domain Computers,dc=handsome,dc=and,dc=smart,dc=com" /REBOOT && goto end || goto move_to_65OU


:move_to_65OU
echo.%computername%|findstr /B /I /C:"65" >nul 2>&1 && netdom JOIN %computername% /Domain:handsome.and.smart.com /userd:delegate /passwordd:topsecret /OU:"ou=XP Client,ou=Domain Computers,dc=handsome,dc=and,dc=smart,dc=com" /REBOOT && goto end || goto move_to_16


:move_to_16
echo.%computername%|findstr /B /I /C:"16" >nul 2>&1 && netdom JOIN %computername% /Domain:handsome.and.smart.com /userd:delegate /passwordd:topsecret /OU:"ou=XP Client,ou=Domain Computers,dc=handsome,dc=and,dc=smart,dc=com" /REBOOT && goto end || goto move_to_104OU 


:move_to_104OU
echo.%computername%|findstr /B /I /C:"104" >nul 2>&1 && netdom JOIN %computername% /Domain:handsome.and.smart.com /userd:delegate /passwordd:topsecret /OU:"ou=104 Computers,dc=handsome,dc=and,dc=smart,dc=com" /REBOOT && goto end || goto move_to_scscOU


:move_to_scscOU
echo.%computername%|findstr /B /I /C:"SCSC" >nul 2>&1 && netdom JOIN %computername% /Domain:handsome.and.smart.com /userd:delegate /passwordd:topsecret /OU:"ou=SCSC COMPUTERS,ou=Domain Computers,dc=handsome,dc=and,dc=smart,dc=com" /REBOOT:22 && goto end || goto default_OU


:move_to_default_OU
::If name does not match any strings, place computer in default container.
echo.%computername%|findstr /B /I /C:"" >nul 2>&1 && netdom JOIN %computername% /Domain:handsome.and.smart.com /userd:delegate /passwordd:topsecret /OU:"OU=DEFAULT CLIENTS,OU=XP ALL OU,dc=handsome,dc=and,dc=smart,dc=com" /REBOOT && goto end



:end

::Pause was used in the attempt to troubleshoot.
pause

Post Reply