Guest User

OCPN: Orbital Cached Profile Nuker v2

a guest
Jun 25th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Winbatch 14.98 KB | None | 0 0
  1. :: Purpose:         A wrapper for Joe Shonks DeleteProfiles.vbs. Deletes old cached user profiles
  2. ::                  on computers listed in the names file or individually. Works with Vista.
  3. :: Requirements:    1. Run this script with a network admin account
  4. ::                  2. Required files:
  5. ::                     - DeleteProfiles.vbs v1.9 or higher, specified in the PAYLOAD variable
  6. ::                     - psexec.exe
  7. ::                  These files may be in any of the following locations:
  8. ::                      a) the directory you run this script from
  9. ::                      b) in the PATH variable
  10. ::                      c) c:\windows\system32\
  11. ::                  3. Put the names of your computers, one per line, in the NAMES_FILE in the same directory as this script
  12. :: Author:          vocatus on reddit.com/r/sysadmin
  13. ::                  Joe Shonk, [email protected] -- DeleteProfiles.vbs
  14. :: History:         2.7 Cleaned up required files check into proper if statements
  15. ::                  2.6 Added function to clear errorlevel in mass upload loop
  16. ::                  2.5 Major code upgrade.
  17. ::                      - Logging function massively improved and debugged
  18. ::                      - Many glitches and failures fixed.
  19. ::                  2.4 skipped
  20. ::                  2.3 Some code cleanup and logging improvement.
  21. ::                  2.2 Changes:
  22. ::                      - Added verbose flag to run-once portion
  23. ::                      - Improved log file rotation section significantly
  24. ::                      - Added ping -n 2 >NUL to the log collector loop, to prevent tripping McAfee
  25. ::                  2.1 Changes:
  26. ::                      - Added log rotation code to auto-archive and age out log files
  27. ::                      - Added "PAYLOAD" variable to represent the DeleteProfiles.vbs script
  28. ::                      - Added check for existence of the .vbs script
  29. ::                  2.0 Complete and major re-write, meant as a complete replacement for OCPN.bat
  30. ::                      - Now uses DeleteProfiles.vbs from Joe Shonk for more accuracy and compatibility
  31. ::                      - Fetches log files from remote computer after operation
  32. ::                      - Logs which registry keys deleted and why they were deleted
  33. ::                      - Can be invoked by specifying either a host or 'all' as the first argument, followed by
  34. ::                        the number of days. Example: OCPN2.bat all 30
  35.  
  36. :: Prep
  37. SETLOCAL
  38. @echo off
  39. cls
  40. set VERSION=2.7
  41. title Orbital Cached Profile Nuker v%VERSION%
  42.  
  43.  
  44. :::::::::::::::
  45. :: VARIABLES :: -- set these
  46. :::::::::::::::
  47. :: Rules for variables:
  48. ::  * NO quotes!                       (bad:  "c:\directory\path"       )
  49. ::  * NO trailing slashes on the path! (bad:   c:\directory\            )
  50. ::  * Spaces are okay                  (okay:  c:\my folder\with spaces )
  51. ::  * Network paths are okay           (okay:  \\server\share name      )
  52. ::                                     (       \\172.16.1.5\share name  )
  53.  
  54. :: Names file is a list of systems you want to act against. One system IP or hostname per line, list can contain both systems and hostnames
  55. set NAMES_FILE=names.txt
  56.  
  57. :: Exclude profiles whos name matches this string
  58. set EXCLUDE_PROFILES=admin*
  59.  
  60. :: Connection timeout in seconds
  61. set TIMEOUT=2
  62.  
  63. :: Name of the script used to do the deleting
  64. set PAYLOAD=DeleteProfiles.vbs
  65.  
  66. :: Log location (directory)
  67. set LOCAL_LOGPATH=C:\Logs
  68.  
  69. :: Note: setting these two variables has no effect, they get clobbered later.
  70. :: This is just to show you what they get set to.
  71. set REMOTE_LOGPATH=\\%TARGET%\C$\Logs
  72. set LOGFILE=%TARGET%_OCPN2.log
  73.  
  74. :: Don't change anything below this line.
  75. :: If you you will break something and somewhere a puppy will die.
  76. set TARGET=%1
  77. set RUN_ONCE=false
  78. set DAYS=%2
  79.  
  80.  
  81. ::::::::::::::::::::::::::
  82. :: REQUIRED FILES CHECK ::
  83. ::::::::::::::::::::::::::
  84.  
  85. :: Test if we're missing PAYLOAD file (the vbs script that does the work)
  86. IF NOT EXIST %PAYLOAD% (
  87.         color 0c
  88.         echo.
  89.         echo  ERROR:
  90.         echo.
  91.         echo  Cannot find %PAYLOAD%. Place %PAYLOAD% in
  92.         echo  the same directory as this script to continue.
  93.         echo.
  94.         pause
  95.         goto end
  96.         )
  97.  
  98. :: Test if we're missing PsExec
  99. IF EXIST psexec.exe goto run_test
  100. IF EXIST "%SystemDrive%\Program Files\SysInternalsSuite\psexec.exe" goto run_test
  101. IF EXIST %WINDIR%\system32\psexec.exe goto run_test
  102. color 0c
  103.     echo.
  104.     echo  ERROR:
  105.     echo.
  106.     echo  Cannot find PsExec.exe. Place PsExec.exe in
  107.     echo  the same directory as this script to continue.
  108.     echo.
  109. pause
  110. goto end
  111.  
  112. :: Test if we're doing a run-once
  113. :run_test
  114. IF '%1%'=='all' goto multiple_pc_run_once
  115. IF NOT '%1%'=='' goto single_pc_run_once
  116. cls
  117.  
  118. :: User notice
  119. :intro
  120. color 0c
  121. echo.
  122. echo  *********************************************************
  123. echo  *                                                       *
  124. echo  *         ORBITAL CACHED PROFILE NUKER (OCPN) v%VERSION%      *
  125. echo  * ----------------------------------------------------- *
  126. echo  * Nuke them from orbit. It's the only way to be sure.   *
  127. echo  *                                                       *
  128. echo  * Windows 2000/XP/Vista caches user profiles at login.  *
  129. echo  * Over time these use up a lot of space. Annoying.      *
  130. echo  *                                                       *
  131. echo  * This script deletes those old profiles.               *
  132. echo  *                                                       *
  133. echo  *********************************************************
  134. echo.
  135. echo  The next screen will let you set the number of days and
  136. echo  the target.
  137. echo.
  138. pause
  139. cls
  140.  
  141. :single_pc
  142. set DAYS=30
  143. color 07
  144. :: Ask user how many days old the profiles should be before getting nuked
  145. echo.
  146. echo  *********************************************************
  147. echo  *                                                       *
  148. echo  *                  IT'S NUKING TIME                     *
  149. echo  * ----------------------------------------------------- *
  150. echo  *                                                       *
  151. echo  * Rules for a safe and happy nuking:                    *
  152. echo  *                                                       *
  153. echo  * 1. Run this script with NETWORK ADMIN rights. Local   *
  154. echo  *    admin rights won't work.                           *
  155. echo  * 2. Run this script from the desktop, NOT a network    *
  156. echo  *    path.                                              *
  157. echo  *                                                       *
  158. echo  * After you enter the target and number of days and hit *
  159. echo  * "enter" the script will begin nuking!                 *
  160. echo  *                                                       *
  161. echo  *********************************************************
  162. echo.
  163. :single_pc_loop
  164. title OCPN2 v%VERSION%
  165. echo.
  166. set /p TARGET=Enter IP, hostname or 'all':
  167.     if %TARGET%==exit goto end
  168. set /P DAYS=  Nuke profiles older than how many days? [%DAYS%]:
  169.     if %DAYS%==exit goto end
  170.     if %TARGET%==all goto multiple_pc_go
  171. set RUN_ONCE=false
  172. goto single_pc_go
  173.  
  174. :: ========================================= ::
  175. ::             SINGLE PC VERSION             ::
  176. :: ========================================= ::
  177.  
  178. :single_pc_run_once
  179. set RUN_ONCE=true
  180. if '%2%'=='' goto single_pc_run_once_need_days
  181. goto single_pc_go
  182.  
  183. :single_pc_run_once_need_days
  184. set DAYS=30
  185. echo.
  186. echo  Target: %TARGET%
  187. echo.
  188. set /P DAYS=  Nuke profiles older than how many days? [%DAYS%]:
  189. if %DAYS%==exit goto end
  190. goto single_pc_go
  191.  
  192. :single_pc_go
  193. title OCPN v%VERSION%: Nuking profiles, please wait...
  194.  
  195. :: Set up our log information here.
  196. set REMOTE_LOGPATH=\\%TARGET%\C$\Logs
  197. set LOGFILE=%TARGET%_OCPN2.log
  198. cls
  199.  
  200. echo.
  201. echo  == Nuking cached profiles older than %DAYS% days on %TARGET%
  202. echo  == Please wait...                    ==
  203. :: delay just so we can see what's going on
  204. ping localhost -n 2 >NUL
  205. echo.
  206. :: Steps
  207. :: 1. a. Make sure our Log directory exists and create it if it doesn't
  208. ::    b. Upload file to remote computer
  209. :: 2. Call cscript.exe and execute the script on the remote computer. We display the verbose
  210. ::    output to the console because we specifically requested this computer.
  211. ::    For the multiple_pc version we don't display the output in the console window since it
  212. ::    would be too noisy, but we DO create a log file and fetch it afterwards.
  213. :: 3. Fetch remote log file & dump it to the Logs directory on this computer
  214. :: 4. Display results
  215. echo.
  216.  
  217. echo  == Uploading script to host...       ==
  218.     ping %TARGET% -n 2 >NUL
  219.     set ERRORLEVEL=0
  220.     IF NOT EXIST %REMOTE_LOGPATH% mkdir %REMOTE_LOGPATH% >NUL
  221.     copy %PAYLOAD% \\%TARGET%\C$ /Y >NUL
  222. echo.
  223. if %ERRORLEVEL%==1 echo  == ERROR: Script failed to upload!   ==
  224. if %ERRORLEVEL%==0 echo  == Script uploaded successfully.     ==
  225. echo.
  226.  
  227.  
  228. echo  == Activating script on host...      ==
  229. echo.
  230. :: Deletes profiles older than X days, and excludes accounts that match what we set earlier in "EXCLUDE_PROFILES."
  231. :: Since we specifically selected this computer, we log AND display output to console
  232. psexec -n %TIMEOUT% \\%TARGET% cscript //nologo C:\%PAYLOAD% /D %DAYS% /E %EXCLUDE_PROFILES% /V /C /L %REMOTE_LOGPATH%\%LOGFILE%
  233. del \\%TARGET%\C$\%PAYLOAD% /F /Q
  234. echo.
  235.  
  236.  
  237. echo  == Fetching log file from host...    ==
  238. set ERRORLEVEL=0
  239. copy %REMOTE_LOGPATH%\%LOGFILE% %LOCAL_LOGPATH% /Y >NUL
  240.     if %ERRORLEVEL%==1 echo  == ERROR: Couldn't fetch logfile!    ==
  241.     if %ERRORLEVEL%==0 echo  == Fetched log successfully.         ==
  242. echo.
  243.  
  244.  
  245. echo  == OCPN Run Complete!                ==
  246. echo.
  247. echo   All profiles %DAYS% days or older were deleted from %TARGET%
  248. echo.
  249. echo   Logfile is at:     %LOCAL_LOGPATH%\%LOGFILE%
  250. echo   Accounts with the text "%EXCLUDE_PROFILES%" in their names were excluded.
  251. echo.
  252. set TARGET=
  253. if %RUN_ONCE%==true goto end
  254. goto single_pc_loop
  255.  
  256.  
  257. REM ================================ REM
  258. REM       MULTIPLE PC VERSION        REM
  259. REM ================================ REM
  260.  
  261. :multiple_pc_run_once
  262. set RUN_ONCE=true
  263. if '%2%'=='' goto multiple_pc_run_once_need_days
  264. goto multiple_pc_go
  265.  
  266. :multiple_pc_run_once_need_days
  267. echo.
  268. echo  Target: All computers listed in %NAMES_FILE%
  269. echo.
  270. set /P DAYS=  Nuke profiles older than how many days? (30 recommended):
  271. if %DAYS%==exit goto end
  272. goto multiple_pc_go
  273.  
  274.  
  275. :multiple_pc_go
  276. title Nuking profiles, please wait...
  277. cls
  278. echo.
  279. echo  LETS ROCK!!
  280. echo.
  281. echo  Will delete cached profiles %DAYS% days and older on all computers
  282. echo  listed in the "%NAMES_FILE%" file.
  283. echo.
  284. echo  ======== Beginning OCPN2 mass run ========
  285. echo.
  286. :: Steps
  287. :: 1. For all machines in the names file:
  288. ::     a. Ping the machine to make sure it's up
  289. ::     b. Create C:\Logs directory if it doesn't exist
  290. ::     c. Upload the %PAYLOAD% script to the computer
  291. :: 2. Call cscript.exe and execute the script on each remote computer.
  292. ::    We start the script then disconnect and move on to the next system.
  293. ::    The downside to this is we don't get to see console output; the upside
  294. ::    is that it's much faster.
  295. :: 3. Wait 30 seconds
  296. :: 4. Go out and fetch log files
  297. :: 5. Compile log files and delete originals with a super-rad FOR loop
  298. :: 6. Results
  299.  
  300. echo  == Uploading to remote targets...       ==
  301. echo.
  302. for /F %%i in (%NAMES_FILE%) do (
  303.     ping %%i -n 1 >NUL
  304.     set ERRORLEVEL=0
  305.     IF NOT EXIST \\%%i\C$\Logs mkdir \\%%i\C$\Logs >NUL
  306.     copy %PAYLOAD% \\%%i\C$ /Y >NUL
  307.     if %ERRORLEVEL%==0 echo     %%i ... OK
  308.     if %ERRORLEVEL%==1 echo     %%i ... FAILED
  309.     set ERRORLEVEL=0
  310.     )
  311. echo.
  312. echo  == Done.                                ==
  313. echo.
  314.  
  315. :: Once we've uploaded the script to all the remote hosts, we go back through
  316. :: the names file and activate it.
  317. ::
  318. :: Verbose logging here just in case something goes horribly wrong.
  319. echo.
  320. echo  == Activating on targets...             ==
  321. echo.
  322. for /F %%i in (%NAMES_FILE%) do (
  323.     ping %%i -n 1 >NUL
  324.     psexec -n %TIMEOUT% -d \\%%i cscript //nologo C:\%PAYLOAD% /D %DAYS% /V /E %EXCLUDE_PROFILES% /L \\%%i\C$\Logs\%COMPUTERNAME%_OCPN2.LOG
  325.     echo.
  326.     )
  327. echo.
  328. echo  == Done.                                ==
  329. echo.
  330. echo  == 2 min cooldown till log fetch...     ==
  331. ping localhost -n 60 >NUL
  332. echo.
  333. echo  == 1 min cooldown remaining...          ==
  334. ping localhost -n 60 >NUL
  335. echo.
  336. echo  == Cooldown done.                       ==
  337. echo.
  338. echo  == Beginning log fetch...               ==
  339. echo.
  340. :: Make a temp directory for compiling logs later
  341. mkdir %TEMP%\OCPN2 >NUL
  342.  
  343. :: We have to use "*" in this loop because the log file is actually saved based on system name, not IP address.
  344. :: It's easy to grab a system-name, but if your names-file is a list of IP addresses instead of system names, it's
  345. :: significantly more effort to parse out IP addresses remotely. It's easier to just grab any file ending in ocpn2.log.
  346. for /F %%i in (%NAMES_FILE%) do (
  347.     ping %%i -n 1 >NUL
  348.     set ERRORLEVEL=0
  349.     copy \\%%i\C$\Logs\*ocpn2.log %TEMP%\OCPN2 /Y >NUL
  350.     del \\%%i\C$\%PAYLOAD% /F /Q
  351.     if %ERRORLEVEL%==0 echo     %%i ... OK
  352.     if %ERRORLEVEL%==1 echo     %%i ... FAILED
  353.     )
  354. echo.
  355. echo  == Done.                                ==
  356. echo.
  357. echo  == Rotating master log...               ==
  358.  
  359. :: Log file rotation. Archives up to 6 backups, ".log" through ".log5".
  360. :: Rotate & age out master logs, then create new blank log
  361. IF EXIST %LOCAL_LOGPATH%\OCPN2_master.log6 del %LOCAL_LOGPATH%\OCPN2_master.log6
  362. IF EXIST %LOCAL_LOGPATH%\OCPN2_master.log5 rename %LOCAL_LOGPATH%\OCPN2_master.log5 OCPN2_master.log6
  363. IF EXIST %LOCAL_LOGPATH%\OCPN2_master.log4 rename %LOCAL_LOGPATH%\OCPN2_master.log4 OCPN2_master.log5
  364. IF EXIST %LOCAL_LOGPATH%\OCPN2_master.log3 rename %LOCAL_LOGPATH%\OCPN2_master.log3 OCPN2_master.log4
  365. IF EXIST %LOCAL_LOGPATH%\OCPN2_master.log2 rename %LOCAL_LOGPATH%\OCPN2_master.log2 OCPN2_master.log3
  366. IF EXIST %LOCAL_LOGPATH%\OCPN2_master.log1 rename %LOCAL_LOGPATH%\OCPN2_master.log OCPN2_master.log2
  367. IF EXIST %LOCAL_LOGPATH%\OCPN2_master.log rename %LOCAL_LOGPATH%\OCPN2_master.log OCPN2_master.log1
  368. echo. > %LOCAL_LOGPATH%\OCPN2_master.log
  369. echo.
  370. echo  == Done.                                ==
  371. echo.
  372. echo  == Compiling log files...               ==
  373.  
  374. :: This loop compiles all the log files into a single master log.
  375. :: It's ugly but it seems to work.
  376. :: For each file in %TEMP%\OCPN2 of type (*), stick it in %%i and DO...this stuff
  377. FOR /r %TEMP%\OCPN2 %%i in (*) DO (
  378.     echo. >> %LOCAL_LOGPATH%\OCPN2_master.log
  379.     echo %%i >>%LOCAL_LOGPATH%\OCPN2_master.log
  380.     echo ========================================================= >>%LOCAL_LOGPATH%\OCPN2_master.log
  381.     type %%i >> %LOCAL_LOGPATH%\OCPN2_master.log
  382.     )
  383.  
  384. echo.
  385. echo  == Done.                                ==
  386. echo.
  387. echo  == Cleaning up...                       ==
  388. rmdir %TEMP%\OCPN2 /S /Q
  389. echo.
  390. echo  == Done.                                ==
  391. echo.
  392. echo  ========= OCPN mass run complete =========
  393. echo.
  394. echo   All profiles %DAYS% days or older were deleted.
  395. echo   Logfile: OCPN2_master.log
  396. echo.
  397. echo   Accounts with the "%EXCLUDE_PROFILES%" in their name were excluded.
  398. echo.
  399. set TARGET=
  400. if %RUN_ONCE%==true goto end
  401. goto single_pc_loop
  402.  
  403. :end
  404. ENDLOCAL
  405. title %USERNAME%
Add Comment
Please, Sign In to add comment