Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for rmuserid.bat

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

  1. @ECHO OFF
  2. IF "%OS%"=="Windows_NT" SETLOCAL
  3.  
  4. :: Windows 2000 or later required
  5. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  6. VER | FIND "Windows NT" >NUL && GOTO Syntax
  7.  
  8. :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
  9. ::  Modify the variables below to match your own environment!  ::
  10. ::  These sample values assume that:                           ::
  11. ::  [1] homedirs are located in \\MYHOMESRV\Users$\            ::
  12. ::  [2] profiles are located in \\MYPROFSRV\Profiles$\ and     ::
  13. ::  [3] TS profiles are located in \\MYTSPRSRV\TsProfiles$     ::
  14. :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
  15. SET DomainCtr=MYDOMCTR
  16. SET HomeDirSrv=MYHOMESRV
  17. SET HomeDirRootShare=Users$
  18. SET ProfileSrv=MYPROFSRV
  19. SET ProfileRootShare=Profiles$
  20. SET TsProfileSrv=MYTSPRSRV
  21. SET TsProfileRootShare=TsProfiles$
  22. :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
  23. ::  Modify the variables above to match your own environment!  ::
  24. :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
  25.  
  26. :: Check command line arguments
  27. IF     "%~1"=="" GOTO Syntax
  28. IF NOT "%~2"=="" GOTO Syntax
  29.  
  30. CLS
  31. ECHO.
  32. ECHO DISCLAIMER
  33. ECHO.
  34. ECHO This is a very powerful and potentially harmful tool.
  35. ECHO.
  36. ECHO Though every effort has been made to make this batch file safe and fool
  37. ECHO proof, it has been tested in one single office network environment only.
  38. ECHO There is no guarantee it will work or be safe in your environment.
  39. ECHO Examine the source code thoroughly before putting it to use.
  40. ECHO Make sure you have a recent and full system state backup available of
  41. ECHO your domain controllers before putting this script to use, and test it
  42. ECHO in a separate test domain first.
  43. ECHO Use this batch file entirely at your own risk.
  44. ECHO.
  45. ECHO I have read and understood the source code, and I accept full responsibility
  46. SET Answer=N
  47. SET /P Answer=for any damage that may result from the use of this script (y/N) 
  48. IF /I NOT "%Answer%"=="Y" GOTO Aborted
  49.  
  50. :: Domain Admin permissions required
  51. NET.EXE USER "%UserName%" /DOMAIN 2>NUL | FIND.EXE "*Domain Admins" >NUL || GOTO Syntax
  52.  
  53. :: No wildcards or whitespace allowed in command line arguments
  54. ECHO "%~1" | FINDSTR.EXE /R /C:"[/?*& ]" >NUL 2>&1 && GOTO Syntax
  55.  
  56. :: Check if all tools used by this batch file are available
  57. DSGET.EXE    /? >NUL 2>&1 || GOTO Syntax
  58. DSQUERY.EXE  /? >NUL 2>&1 || GOTO Syntax
  59. RMTSHARE.EXE /? >NUL 2>&1 || GOTO DownloadRK
  60. TAKEOWN.EXE NUL 2>&1 | FIND.EXE /I "Successful"                 >NUL || GOTO DownloadRK
  61. TSCMD.EXE    /? 2>&1 | FIND.EXE "http://www.systemtools.com"    >NUL || GOTO DownloadTsCmd
  62. CALL OWN.BAT    2>&1 | FIND.EXE "http://www.robvanderwoude.com" >NUL || GOTO DownloadOwn
  63.  
  64. :: Confirm this is the correct account
  65. ECHO.
  66. ECHO Remove User ID . . .
  67. ECHO.
  68. DSQUERY.EXE User -samid %1 2>NUL | FIND.EXE "=" || GOTO Syntax
  69. ECHO.
  70. SET Answer=N
  71. SET /P Answer=Do you really want to remove this account? (y/N) 
  72. ECHO.
  73. IF /I NOT "%Answer%"=="Y" GOTO Aborted
  74.  
  75. :: Parse HomeDir and HomeShare if applicable
  76. SET HomeDir=
  77. SET HomeShare=
  78. SET ShareDir=
  79. FOR /F "tokens=1" %%A IN ('DSQUERY.EXE User -samid %1 2^>NUL ^| DSGET.EXE User -hmdir 2^>NUL ^| FINDSTR.EXE /R /I /C:"\\\\%HomeDirSrv%\\%1\$"') DO SET HomeDir=%%A
  80. IF DEFINED HomeDir SET HomeShare=%HomeDir%
  81. FOR /F "tokens=1" %%A IN ('DSQUERY.EXE User -samid %1 2^>NUL ^| DSGET.EXE User -hmdir 2^>NUL ^| FINDSTR.EXE /R /I /C:"\\\\%HomeDirSrv%\\%HomeDirRootShare:$=\$%\\%1"') DO SET HomeDir=%%A
  82. IF NOT DEFINED HomeDir (
  83. 	ECHO No homedir defined for this account.
  84. 	GOTO Profile
  85. )
  86. :: Find the true path of a shared HomeDir if applicable
  87. IF DEFINED HomeShare FOR /F "tokens=1*" %%A IN ('RMTSHARE.EXE %HomeShare% 2^>NUL ^| FINDSTR.EXE /R /B /I /C:"Path "') DO SET ShareDir=%%B
  88. IF DEFINED ShareDir SET HomeDir=\\%HomeDirSrv%\%ShareDir::=$%
  89. :: Confirm deletion
  90. SET Answer=N
  91. SET /P Answer=Delete home directory %HomeDir%? (y/N) 
  92. ECHO.
  93. IF /I NOT "%Answer%"=="Y" GOTO Profile
  94. IF DEFINED HomeShare (
  95. 	ECHO Deleting home share %HomeShare% . . .
  96. 	RMTSHARE.EXE %HomeShare% /DELETE
  97. )
  98. ECHO Deleting home directory %HomeDir% . . .
  99. RD /S /Q %HomeDir%
  100.  
  101. :Profile
  102. SET Profile=
  103. FOR /F "tokens=1" %%A IN ('DSQUERY.EXE User -samid %1 2^>NUL ^| DSGET.EXE User -profile 2^>NUL ^| FINDSTR.EXE /R /I /C:"\\\\%ProfileSrv%\\%ProfileRootShare:$=\$%\\%1"') DO SET Profile=%%A
  104. IF NOT DEFINED Profile (
  105. 	ECHO No profile defined for this account.
  106. 	GOTO TsProfile
  107. )
  108. IF NOT EXIST %Profile% (
  109. 	ECHO Profile not found.
  110. 	GOTO TsProfile
  111. )
  112. :: Confirm deletion
  113. ECHO.
  114. SET Answer=N
  115. SET /P Answer=Delete profile directory %Profile%? (y/N) 
  116. ECHO.
  117. IF /I NOT "%Answer%"=="Y" GOTO TsProfile
  118. ECHO Deleting profile, please wait . . .
  119. CALL OWN.BAT %Profile% ^>NUL 2^>^&1
  120. RD /S /Q %Profile%
  121. IF EXIST %Profile% (
  122. 	FOR /F "tokens=*" %%A IN ('DIR /A-D /B /S "%Profile%\*.*"') DO (TAKEOWN.EXE "%%~A" >NUL 2>&1)
  123. 	RD /S /Q %Profile%
  124. )
  125. ECHO Profile deleted.
  126.  
  127. :TsProfile
  128. SET TsProfile=
  129. FOR /F "tokens=*" %%A IN ('TSCMD.EXE %DomainCtr% %1 TerminalServerProfilePath 2^>NUL') DO SET TsProfile=%%A
  130. IF NOT DEFINED TsProfile (
  131. 	ECHO No Terminal Server profile defined for this account.
  132. 	GOTO UserID
  133. )
  134. IF NOT EXIST %TsProfile% (
  135. 	ECHO Terminal Server profile not found.
  136. 	GOTO UserID
  137. )
  138. ECHO.
  139. SET Answer=N
  140. SET /P Answer=Delete Terminal Server profile %TsProfile%? (y/N) 
  141. ECHO.
  142. IF /I NOT "%Answer%"=="Y" GOTO UserID
  143. ECHO Deleting Terminal Server profile, please wait . . .
  144. CALL OWN.BAT %TsProfile% ^>NUL 2^>^&1
  145. RD /S /Q %TsProfile%
  146. IF EXIST %TsProfile% (
  147. 	FOR /F "tokens=*" %%A IN ('DIR /A-D /B /S "%TsProfile%\*.*"') DO (TAKEOWN.EXE "%%~A" >NUL 2>&1)
  148. 	RD /S /Q %TsProfile%
  149. )
  150. ECHO Terminal Server profile deleted.
  151.  
  152. :UserID
  153. ECHO.
  154. SET Answer=N
  155. SET /P Answer=Delete User ID %1? (y/N) 
  156. ECHO.
  157. IF /I NOT "%Answer%"=="Y" GOTO End
  158. NET USER %1 /DOMAIN /DEL
  159.  
  160. :: Done
  161. GOTO End
  162.  
  163.  
  164. :Aborted
  165. ECHO.
  166. ECHO Procedure aborted by user request.
  167. GOTO End
  168.  
  169.  
  170. :DownloadRK
  171. ECHO.
  172. ECHO Unable to find the Resource Kit tools RMTSHARE and TAKEOWN.
  173. ECHO You need to download and install these tools in order to use this batch file.
  174. ECHO.
  175. SET Answer=N
  176. SET /P Answer=Do you want to download these tools from the Microsoft website? (y/N) 
  177. IF /I "%Answer%"=="Y" START "Download NT4 Resource Kit" "http://www.microsoft.com/ntserver/nts/downloads/recommended/ntkit/"
  178. ECHO.
  179. ECHO Install the downloaded Resource Kit tools and try again.
  180. GOTO End
  181.  
  182.  
  183. :DownloadTsCmd
  184. ECHO.
  185. ECHO Unable to find the TSCMD tool.
  186. ECHO You need to download and install this tool in order to use this batch file.
  187. ECHO.
  188. SET Answer=N
  189. SET /P Answer=Do you want to download TSCMD from the SystemTools website? (y/N) 
  190. IF /I "%Answer%"=="Y" START "Download TSCMD" "http://www.systemtools.com/free_frame.htm"
  191. ECHO.
  192. ECHO Install TSCMD and try again.
  193. GOTO End
  194.  
  195. :DownloadOwn
  196. ECHO.
  197. ECHO Unable to find OWN.BAT.
  198. ECHO You need to download and install this tool in order to use this batch file.
  199. ECHO.
  200. SET Answer=N
  201. SET /P Answer=Do you want to download OWN.BAT from this website? (y/N) 
  202. IF /I "%Answer%"=="Y" START "Download OWN.BAT" "http://www.robvanderwoude.com/files/owner.zip"
  203. ECHO.
  204. ECHO Unzip the downloaded OWNER.ZIP and copy OWN.BAT to a directory in the PATH.
  205. ECHO Then try again.
  206. GOTO End
  207.  
  208.  
  209. :Syntax
  210. CLS
  211. ECHO.
  212. ECHO RmUserID.bat,  Version 0.52 beta
  213. ECHO Remove user account and associated home directory, home share, and profiles
  214. ECHO.
  215. ECHO Usage:  RMUSERID  user_id
  216. ECHO.
  217. ECHO Where:  user_id  is the (SAM) user ID to be deleted
  218. ECHO.
  219. ECHO Notes:  This batch file will ask for confirmation at each step of the process.
  220. ECHO         Please verify each name and path before pressing Y and Enter.
  221. ECHO         To run this script you need Domain Admin permissions.
  222. ECHO         You also need the following tools:
  223. ECHO         DSGET and DSQUERY (native in Windows Server 2003, will work
  224. ECHO         in Windows 2000), RMTSHARE and TAKEOWN (Windows NT 4 Server
  225. ECHO         Resource Kit), TSCMD (http://www.systemtools.com) and OWN.BAT
  226. ECHO         (http://www.robvanderwoude.com).
  227. ECHO         The batch file will prompt you for download if any of these is missing.
  228. ECHO         The batch file aborts if the user ID contains spaces or ampersands.
  229. ECHO.
  230. ECHO Written by Rob van der Woude
  231. ECHO http://www.robvanderwoude.com
  232. ECHO.
  233. PAUSE
  234. ECHO.
  235. ECHO DISCLAIMER
  236. ECHO.
  237. ECHO Though every effort has been made to make this batch file safe and fool
  238. ECHO proof, it has been tested in one single office network environment only.
  239. ECHO There is no guarantee it will work or be safe in your environment.
  240. ECHO Examine the source code thoroughly before putting it to use.
  241. ECHO Make sure you have a recent and full system state backup available of
  242. ECHO your domain controllers before putting this script to use, and test it
  243. ECHO in a separate test domain first.
  244. ECHO Use this batch file entirely at your own risk.
  245. ECHO.
  246. PAUSE
  247.  
  248. :End
  249. IF "%OS%"=="Windows_NT" ENDLOCAL
  250.  

page last uploaded: 2017-07-06, 12:37