Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for setown.bat

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

  1. @ECHO OFF
  2. :: Check if running in Windows Server 2003
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4. VER | FIND.EXE "5.2.3790]" >NUL || GOTO Syntax
  5.  
  6. :: User ID should be specified on the command line
  7. IF     "%~1"=="" GOTO Syntax
  8. IF NOT "%~2"=="" GOTO Syntax
  9. ECHO."%~1" | FIND.EXE "?" >NUL && GOTO Syntax
  10.  
  11. :: Check if logged on to a domain
  12. IF "%UserDomain%"=="%ComputerName% GOTO NoDomain
  13.  
  14. :: Check validity of specified user ID
  15. DSQUERY.EXE User -samid "%~1" 2>NUL | FIND.EXE "=" >NUL || GOTO UserNotFound
  16.  
  17. :: Check availablility of third party tools
  18. SET Abort=0
  19. TSCMD.EXE        /? 2>&1 | FIND.EXE /I "http://www.systemtools.com" >NUL || CALL :DownloadTsCmd
  20. CHOWN.EXE        /? 2>&1 | FIND.EXE /I "Alexander Frink"            >NUL || CALL :DownloadChOwn
  21. NLTEST.EXE       /? 2>&1 | FIND.EXE /I "/QUERY"                     >NUL || CALL :InstallNlTest
  22. VER | XCACLS.EXE /? 2>&1 | FIND.EXE /I "Permissions"                >NUL || CALL :DownloadXCacls
  23. IF "%Abort%"=="1" GOTO:EOF
  24.  
  25. :: Check again
  26. TSCMD.EXE  /? 2>&1 | FIND.EXE "http://www.systemtools.com" >NUL || GOTO Syntax
  27. CHOWN.EXE  /? 2>&1 | FIND.EXE "Alexander Frink"            >NUL || GOTO Syntax
  28. NLTEST.EXE /? 2>&1 | FIND.EXE "/QUERY"                     >NUL || GOTO Syntax
  29. VER | XCACLS.EXE /? 2>&1 | FIND.EXE "XCACLS"               >NUL || GOTO Syntax
  30.  
  31. :: Keep variables local
  32. SETLOCAL
  33.  
  34. :: Retrieve PDC name; delims is a dot, followed by a space and a tab
  35. FOR /F "tokens=1 delims=. 	" %%A IN ('NLTEST.EXE /DSGETDC:%UserDomain% 2^>NUL ^| FIND.EXE "[PDC]"') DO SET PDC=%%A
  36. IF NOT DEFINED PDC GOTO NoPdcFound
  37.  
  38. :: Retrieve user's profile path
  39. FOR /F "tokens=*" %%A IN ('DSQUERY.EXE User -samid "%~1" 2^>NUL ^| DSGET.EXE User -profile 2^>NUL ^| FIND.EXE "\"') DO SET Profile=%%A
  40.  
  41. :: Retrieve user's Terminal Server profile path
  42. FOR /F "tokens=*" %%A IN ('TSCMD.EXE %PDC% "%~1" TerminalServerProfilePath 2^>NUL') DO FOR %%B IN (%%A) DO SET TsProfile=%%B
  43.  
  44. :: Set ownership and permissions for profile directories
  45. IF DEFINED TsProfile CALL :Own "%TsProfile%"
  46. IF DEFINED Profile   CALL :Own "%Profile%"
  47.  
  48. :: Done
  49. ENDLOCAL
  50. GOTO:EOF
  51.  
  52.  
  53. :Own
  54. :: Display ownership and permissions before, for optional logging
  55. DIR /Q "%~1*"
  56. CACLS.EXE "%~1"
  57. :: Take ownership -- to make absolutely sure this will succeed, use both TAKEOWN and CHOWN
  58. TAKEOWN.EXE /F "%~1" /A
  59. CHOWN.EXE -r -q Administrators "%~1\*.*"
  60. CHOWN.EXE -r -q Administrators "%~1\NTUSER.*.*"
  61. CHOWN.EXE -r -q Administrators "%~1\Application Data"
  62. FOR %%A IN (DAT DAT.LOG INI POL) DO TAKEOWN.EXE /F "%~1\NTUSER.%%A" /A
  63. :: Set permissions -- Administrators, SYSTEM and the user himself, all Full control
  64. ECHO Y| CACLS.EXE "%~f1" /T /C /G "BUILTIN\Administrators":F
  65. XCACLS.EXE "%~f1" /T /E /C /G "NT AUTHORITY\SYSTEM":F
  66. XCACLS.EXE "%~f1" /T /E /C /G "%UserDomain%\%~n1":F
  67. :: Return ownership to user
  68. CHOWN.EXE -r -q -d %UserDomain% "%~n1" "%~f1\*.*"
  69. :: Display ownership and permissions afterwards, for optional logging
  70. DIR /Q "%~1*"
  71. CACLS.EXE "%~1"
  72. ECHO.
  73. GOTO:EOF
  74.  
  75.  
  76. :DownloadTsCmd
  77. SET Answer=N
  78. ECHO This batch file requires TsCmd to retrieve the Terminal Server Profile path.
  79. SET /P Answer=Would you like to download this free tool now? [y/N] 
  80. IF /I "%Answer%"=="Y" (
  81. 	START http://www.systemtools.com/free_frame.htm
  82. 	ECHO Install TsCmd after downloading it and
  83. 	PAUSE
  84. ) ELSE (
  85. 	SET Abort=1
  86. )
  87. GOTO:EOF
  88.  
  89.  
  90. :DownloadChOwn
  91. SET Answer=N
  92. ECHO This batch file requires ChOwn to set ownership of files and directories.
  93. SET /P Answer=Would you like to download this free tool now? [y/N] 
  94. IF /I "%Answer%"=="Y" (
  95. 	START http://wwwthep.physik.uni-mainz.de/~frink/chown/readme.html
  96. 	ECHO Install ChOwn after downloading it and
  97. 	PAUSE
  98. ) ELSE (
  99. 	SET Abort=1
  100. )
  101. GOTO:EOF
  102.  
  103.  
  104. :DownloadXCacls
  105. SET Answer=N
  106. ECHO This batch file requires XCAcls to change permissions for files and directories.
  107. SET /P Answer=Would you like to download this free tool now? [y/N] 
  108. IF /I "%Answer%"=="Y" (
  109. 	START http://www.microsoft.com/windows2000/techinfo/reskit/tools/existing/xcacls-o.asp
  110. 	ECHO Install XCAcls after downloading it and
  111. 	PAUSE
  112. ) ELSE (
  113. 	SET Abort=1
  114. )
  115. GOTO:EOF
  116.  
  117.  
  118. :InstallNlTest
  119. SET Answer=N
  120. ECHO This batch file requires NlTest to retrieve the PDC name
  121. SET /P Answer=Would you like to install this tool from your Windows CD-ROM now? [y/N] 
  122. IF /I "%Answer%"=="Y" (
  123. 	ECHO Insert your Windows server CD-ROM and install z:\Support\Tools\NLTEST.EXE,
  124. 	ECHO where z: should be replaced by the actual CD-ROM drive letter.
  125. 	PAUSE
  126. ) ELSE (
  127. 	SET Abort=1
  128. )
  129. GOTO:EOF
  130.  
  131.  
  132. :NoDomain
  133. ECHO.
  134. ECHO This batch file requires Domain Administrator access.
  135. ECHO However, your computer is not even a domain member.
  136. ECHO Log on to a domain as a Domain Administrator and try again.
  137. GOTO Syntax
  138.  
  139.  
  140. :UserNotFound
  141. ECHO.
  142. ECHO Unknown user ID: %UserDomain%\%~1
  143.  
  144.  
  145. :Syntax
  146. ECHO.
  147. ECHO SetOwn.bat, Version 1.00 for Windows Server 2003
  148. ECHO Restore ownership and permissions for roaming ^(TS^) profile directories
  149. ECHO.
  150. ECHO Usage:  SETOWN   user_id
  151. ECHO.
  152. ECHO Where:  user_id  is the ^(SAM^) ID of the domain user whose roaming
  153. ECHO                  ^(TS^) profile directories need to be corrected
  154. ECHO.
  155. ECHO Notes:  This batch file can only be run on a Windows Server 2003 server in a
  156. ECHO         domain by a Domain Administrator.
  157. ECHO         This batch files requires the following ^(third party^) support tools:
  158. ECHO         CHOWN ^(http://wwwthep.physik.uni-mainz.de/~frink/chown/readme.html^),
  159. ECHO         TSCMD ^(http://www.systemtools.com/^), XCACLS ^(http://www.microsoft.com/
  160. ECHO         windows2000/techinfo/reskit/tools/existing/xcacls-o.asp^) and NLTEST
  161. ECHO         ^(\SUPPORT\TOOLS directory on the Windows server CD_ROM^). You will be
  162. ECHO         prompted to download and/or install it if it isn't available.
  163. ECHO         This is a powerful tool! Use it entirely at your own risk, and only if
  164. ECHO         you understand what it will do and how. Do not use without testing.
  165. ECHO         Have a recent, tested, full backup plus restore software ready.
  166. ECHO.
  167. ECHO Written by Rob van der Woude
  168. ECHO http://www.robvanderwoude.com
  169.  

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