Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for cloneusr.bat

(view source code of cloneusr.bat as plain text)

  1. @ECHO OFF
  2. :: Clear screen
  3. CLS
  4.  
  5. :: Check Windows version: Windows 2000 or later required
  6. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  7. VER | FIND.EXE "Windows NT" >NUL
  8. IF NOT ERRORLEVEL 1 GOTO Syntax
  9.  
  10. :: Keep variables local
  11. SETLOCAL
  12.  
  13. :: Reset variables
  14. FOR %%A IN (CC DC Domain Download FN HomeDrv HomeDir LN Profile samid TemplateHmDir TsCmdAvailable TsProfile) DO (SET %%A=)
  15.  
  16. :: Check if The DS Tools are available
  17. DSQUERY.EXE /? >NUL 2>&1
  18. IF ERRORLEVEL 1 (
  19. 	ECHO.
  20. 	ECHO This batch files uses Windows Server 2003's native Directory Services Tools
  21. 	ECHO to modify user objects in Active Directory. Please make sure that DSQUERY.EXE,
  22. 	ECHO DSGET.EXE, DSMOD.EXE and DSMOVE.EXE are available on your computer.
  23. 	GOTO Syntax
  24. )
  25.  
  26. :: Check if TSCMD is available
  27. TSCMD.EXE 2>NUL | FIND.EXE /I "www.systemtools.com" >NUL
  28. IF ERRORLEVEL 1 (
  29. 	SET TsCmdAvailable=No
  30. 	ECHO This batch file requires SystemTools.com's TSCMD utility.
  31. 	SET /P Download=Do you want to download it now? [y/N] 
  32. )
  33.  
  34. :: Start download if confirmed
  35. IF /I "%Download%"=="Y" (
  36. 	START "TsCmd" "http://www.systemtools.com/free_frame.htm"
  37. 	ECHO.
  38. 	ECHO Install the downloaded file and make sure TSCMD.EXE is in the PATH.
  39. 	ECHO Then try again.
  40. )
  41.  
  42. :: Abort if TSCMD.EXE is not available yet
  43. IF "%TsCmdAvailable%"=="No" GOTO:EOF
  44.  
  45.  
  46. :: Check command line
  47. FOR %%A IN (%*) DO (CALL :Arguments %%A)
  48.  
  49. :: Abort on missing info
  50. IF NOT DEFINED samid GOTO Syntax
  51. IF NOT DEFINED FN    GOTO Syntax
  52. IF NOT DEFINED LN    GOTO Syntax
  53. IF NOT DEFINED CC    GOTO Syntax
  54.  
  55. :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
  56. :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
  57. ::                                                                         ::
  58. ::   The following path settings should be modified for your own domain    ::
  59. ::                                                                         ::
  60. :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
  61. :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
  62. :: Set homedir and profiles path
  63. SET HomeDrv=H:
  64. SET HomeDir=\\MyServer\Users\%samid%
  65. SET Profile=\\MyServer\Profile$\%samid%
  66. SET TsProfile=\\MyServer\TsProfile$\%samid%
  67. SET TemplateHmDir=\\MyServer\Users\Template
  68.  
  69. :: Abort if this computer is not a domain member
  70. IF /I "%UserDomain%"=="%ComputerName%" GOTO Syntax
  71.  
  72. :: Retrieve domain controller name
  73. FOR /F "tokens=2 delims==," %%A IN ('DSQUERY.EXE Server') DO SET DC=%%A
  74. IF NOT DEFINED DC GOTO Syntax
  75.  
  76. :: Check if middle initials is specified
  77. IF DEFINED MI (
  78. 	SET MIParm=-mi %MI%
  79. 	SET MI= %MI%
  80. )
  81.  
  82. :: Create user account on domain
  83. ECHO Creating new domain user account for %samid% . . .
  84. NET.EXE USER %samid% password /FULLNAME:"%FN%%MI% %LN%" /DOMAIN /ADD
  85. ECHO.
  86.  
  87. :: Store parent OU and UserDNs for both accounts in variables
  88. FOR /F "tokens=*" %%A IN ('DSQUERY.EXE User -samid %samid% 2^>NUL') DO SET UserDN=%%A
  89. FOR /F "tokens=*" %%A IN ('DSQUERY.EXE User -samid %CC%    2^>NUL') DO SET UserDNCC=%%A
  90. FOR /F "tokens=1* delims=," %%A IN ('ECHO.%UserDNCC%') DO SET OU="%%~B
  91.  
  92. :: Define roaming profile for new account only if account to be cloned has a roaming profile
  93. DSGET.EXE User %UserDN% -profile 2>NUL | FIND.EXE /I "\\" >NUL
  94. IF ERRORLEVEL 1 (SET ProfileParm=) ELSE (SET ProfileParm=-profile %Profile%)
  95.  
  96. :: Clone login script setting
  97. FOR /F %%A IN ('DSGET.EXE User %UserDN% -loscr 2^>NUL ^| FIND.EXE "."') DO SET LoginScr=%%A
  98. IF DEFINED LoginScr (SET LoginParm=-loscr %LoginScr%) ELSE (SET LoginParm=)
  99.  
  100. :: Set first name, middle initials and last name, homedir and must-change-password for the new account
  101. ECHO Modifying profile and account settings  . . .
  102. DSMOD.EXE User %UserDN% -fn "%FN%" %MIParm% -ln "%LN%" -hmdrv %HomeDrv% -hmdir %HomeDir% -mustchpwd yes -canchpwd yes %ProfileParm% %LoginParm%
  103. ECHO.
  104.  
  105. :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
  106. :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
  107. ::                                                                                     ::
  108. ::   The following settings may need to be modified depending on your homedir server   ::
  109. ::                                                                                     ::
  110. :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
  111. :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
  112. :: Create homedir if necessary, and set permissions
  113. ECHO Setting permissions on homedir . . .
  114. IF NOT EXIST %HomeDir% MD %HomeDir%
  115. CACLS.EXE %HomeDir% /E /P %UserDomain%\%samid%:C
  116. CACLS.EXE %HomeDir% /E /R BUILTIN\Users
  117. CACLS.EXE %HomeDir% /E /R "CREATOR OWNER"
  118. CACLS.EXE %HomeDir% /E /R "NT AUTHORITY\SYSTEM"
  119. ECHO.
  120.  
  121. :: Set TsHomedir and TsProfile
  122. ECHO Setting Terminal Services profile . . .
  123. TSCMD.EXE %DC% %samid% AllowLogonTerminalServer 1
  124. TSCMD.EXE %DC% %samid% TerminalServerProfilePath %TsProfile%
  125. TSCMD.EXE %DC% %samid% TerminalServerHomeDir %HomeDir%
  126. TSCMD.EXE %DC% %samid% TerminalServerHomeDirDrive %HomeDrv%
  127. ECHO.
  128.  
  129. :: Clone group memberships:
  130. :: list group memberships for samid2 / filter out Domain Users to prevent DSMOD error message / add new account to group members
  131. ECHO Cloning %CC%'s group memberships . . .
  132. DSGET.EXE User %UserDNCC% -memberof | FIND.EXE /V /I "=Domain Users" | DSMOD.EXE Group -addmbr %UserDN%
  133. ECHO.
  134.  
  135. :: Move new user object to samid2's parent OU, and change UserDN variable accordingly
  136. ECHO Moving new user object to parent OU . . .
  137. DSMOVE.EXE %UserDN% -newparent %OU%
  138. FOR /F "tokens=*" %%A IN ('DSQUERY.EXE User -samid %samid% 2^>NUL') DO SET UserDN=%%A
  139. ECHO.
  140.  
  141. :: Copy template homedir
  142. IF DEFINED TemplateHmDir (
  143. 	ECHO Copying template homedir . . .
  144. 	XCOPY.EXE %TemplateHmDir%\*.* %HomeDir%.\*.* /D /H /R /S /Y >NUL
  145. 	ECHO.
  146. )
  147.  
  148. :: Display summary of results
  149. ECHO Summary for new user account:
  150. DSGET User %UserDN% -dn
  151. DSGET User %UserDN% -samid -display
  152. DSGET User %UserDN% -profile -hmdir
  153. CACLS.EXE %HomeDir%
  154. ECHO.
  155.  
  156. ENDLOCAL
  157. GOTO:EOF
  158.  
  159.  
  160. :Arguments
  161. SET Argument=%~1
  162. ECHO.%~1 | FIND.EXE "/" >NUL || (SET samid=%~1)
  163. IF /I "%Argument:~0,3%"=="/CC" FOR /F "tokens=2 delims=:" %%B IN ('ECHO.%1') DO (SET CC=%%~B)
  164. IF /I "%Argument:~0,3%"=="/FN" FOR /F "tokens=2 delims=:" %%B IN ('ECHO.%1') DO (SET FN=%%~B)
  165. IF /I "%Argument:~0,3%"=="/LN" FOR /F "tokens=2 delims=:" %%B IN ('ECHO.%1') DO (SET LN=%%~B)
  166. IF /I "%Argument:~0,3%"=="/MI" FOR /F "tokens=2 delims=:" %%B IN ('ECHO.%1') DO (SET MI=%%~B)
  167. GOTO:EOF
  168.  
  169.  
  170. :Syntax
  171. ECHO.
  172. ECHO CloneUsr.bat,  Version 1.11
  173. ECHO Create a new domain user ID by cloning an existing one
  174. ECHO.
  175. ECHO Usage: CLONEUSR samid /FN:firstname [/MI:middleinitial] /LN:lastname /CC:samid2
  176. ECHO.
  177. ECHO Where: samid  = new user ID
  178. ECHO        samid2 = user ID to be cloned
  179. ECHO.
  180. ECHO Notes: You MUST modify the hard coded domain name and homedir and profile paths
  181. ECHO        in this batch file before using it; read the comments in the code.
  182. ECHO        This batch file uses Windows Server 2003's DS Tools ^& SystemTools.com's
  183. ECHO        TSCMD; if the latter isn't available, you will be prompted for download.
  184. ECHO        Only a limited selection of attributes for user objects is cloned by
  185. ECHO        this batch file; use the DS Tools' help to modify this batch file and
  186. ECHO        clone more attributes ^(www.robvanderwoude.com/adshelp/adshelp.html^)
  187. ECHO.
  188. ECHO Written by Rob van der Woude
  189. ECHO http://www.robvanderwoude.com
  190. IF "%OS%"=="Windows_NT" ENDLOCAL
  191.  

page last modified: 2024-04-16; loaded in 0.0091 seconds