使用netdom自动批处理脚本将新映像的计算机加入域

时间:2022-07-17 13:53:34

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
  • Windows 2003即将成为2008 R2 AD架构

  • XP SP3 workstations soon to be Windows 7 (like in a year or two).
  • XP SP3工作站很快就会成为Windows 7(就像一两年一样)。

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.

在使用GhostCast服务器或GSS版本2.5的Ghost控制台进行SysPrep后,我使用了几个批处理脚本和几个vbs文件。基本上,WSNAME通过文本文件中的mac地址重命名计算机并重新启动。

A program has to update its SQL DB instance after the computer is renamed, then the computer has to reboot again.

程序必须在重命名计算机后更新其SQL数据库实例,然后计算机必须重新启动。

After this reboot I would like for this script to perform the following ALL AUTOMATED:

在此重新启动后,我希望此脚本执行以下所有自动执行:

A batch script using netdom is preferred.

使用netdom的批处理脚本是首选。

  1. Queries the computer name via string or whatever is appropriate
  2. 通过字符串或任何适当的查询来查询计算机名称

  3. If computer name matches string, join this computer to this domain and OU then reboot and script should end or
  4. 如果计算机名称与字符串匹配,请将此计算机加入此域,然后重新启动OU,脚本应该结束或

  5. 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
  6. 如果计算机名称不匹配,请转到下一个语句以检查匹配并将此计算机加入此域,然后重新启动OU,脚本应该结束或

  7. If no string matches are found, recurse through statements and join computer to the default OU then reboot and script should end
  8. 如果找不到匹配的字符串,则通过语句递归并将计算机连接到默认OU,然后重新启动并且脚本应该结束

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

实质上,所有计算机都将在指定的OU下加入域。

The important stuff has been changed to protect the innocent!

重要的东西已被改变,以保护无辜!

This attempt was pooled from several websites but primarily from 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.

此尝试汇集自多个网站,但主要来自查明环境变量是否包含子字符串。如果使用vbs或任何其他程序可以做得更好,请提供文件。

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.

主要问题似乎是根据findstr命令获取正确的语法来查询计算机名。我可以操作语句以使它们通过命令行手动工作,但不能作为一个批处理脚本。

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.

经过更多的研究,似乎可能使用Set命令为搜索字符串设置变量也可能是合理的。其中一些计算机属于同一个OU,因此只要考虑了正确的查询字符串,就可以修改或省略其中的一些语句。

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

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

1 个解决方案

#1


0  

I wouldn't be using | except for in certain cases when it really does name sense, this is not one of them. Batch has always had issues piping.

我不会用|除了在某些情况下,它确实有名称意义,这不是其中之一。批次一直有问题管道。

I'd do a re-write using standard function calls (use :Tag nomenclature to title your functions and GOTO :EOF to terminate them (with a :EOF Tag at the bottom). Use Call and Goto commands to move around. You can extract the required information from the system and reliably inject into a variable from there writing to files and using FOR /F to pull.

我会使用标准函数调用进行重写(使用:标记命名法来标题你的函数和GOTO:EOF来终止它们(在底部有一个:EOF标签)。使用Call和Goto命令移动。你可以从系统中提取所需的信息,并从写入文件并使用FOR / F拉取可靠地注入变量。

The only time you need to use the %% is when using FOR /F.

您需要使用%%的唯一时间是使用FOR / F.

There's a few ways to Domain join a system; NetDom, WMIC, Powershell, VBS functions, et-cetra. My preference is to use the newest method and externalize it into a script (e.g. drop the data into a text file then call a powershell function which returns a variable the other batch reads to determine what goes on). Batch was really built to be used that way.

域加入系统有几种方法; NetDom,WMIC,Powershell,VBS功能,et-cetra。我的偏好是使用最新的方法并将其外部化为一个脚本(例如,将数据放入一个文本文件,然后调用一个powershell函数,该函数返回另一个批处理读取的变量以确定发生了什么)。 Batch真的是以这种方式使用的。

Finally, I'd have a script on the server to move the computers into the appropriate OU once joined. Doing it in the host script can be messy if you make a mistake and catch it during deployment. It's one more button to press, no big deal. Also lets you use deployment Group policy settings.

最后,我在服务器上有一个脚本,一旦加入,就将计算机移动到相应的OU中。如果您犯了错误并在部署期间捕获它,那么在主机脚本中执行此操作可能会很麻烦。这是按下的另一个按钮,没什么大不了的。还允许您使用部署组策略设置。

#1


0  

I wouldn't be using | except for in certain cases when it really does name sense, this is not one of them. Batch has always had issues piping.

我不会用|除了在某些情况下,它确实有名称意义,这不是其中之一。批次一直有问题管道。

I'd do a re-write using standard function calls (use :Tag nomenclature to title your functions and GOTO :EOF to terminate them (with a :EOF Tag at the bottom). Use Call and Goto commands to move around. You can extract the required information from the system and reliably inject into a variable from there writing to files and using FOR /F to pull.

我会使用标准函数调用进行重写(使用:标记命名法来标题你的函数和GOTO:EOF来终止它们(在底部有一个:EOF标签)。使用Call和Goto命令移动。你可以从系统中提取所需的信息,并从写入文件并使用FOR / F拉取可靠地注入变量。

The only time you need to use the %% is when using FOR /F.

您需要使用%%的唯一时间是使用FOR / F.

There's a few ways to Domain join a system; NetDom, WMIC, Powershell, VBS functions, et-cetra. My preference is to use the newest method and externalize it into a script (e.g. drop the data into a text file then call a powershell function which returns a variable the other batch reads to determine what goes on). Batch was really built to be used that way.

域加入系统有几种方法; NetDom,WMIC,Powershell,VBS功能,et-cetra。我的偏好是使用最新的方法并将其外部化为一个脚本(例如,将数据放入一个文本文件,然后调用一个powershell函数,该函数返回另一个批处理读取的变量以确定发生了什么)。 Batch真的是以这种方式使用的。

Finally, I'd have a script on the server to move the computers into the appropriate OU once joined. Doing it in the host script can be messy if you make a mistake and catch it during deployment. It's one more button to press, no big deal. Also lets you use deployment Group policy settings.

最后,我在服务器上有一个脚本,一旦加入,就将计算机移动到相应的OU中。如果您犯了错误并在部署期间捕获它,那么在主机脚本中执行此操作可能会很麻烦。这是按下的另一个按钮,没什么大不了的。还允许您使用部署组策略设置。