Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :: Purpose: A wrapper for Joe Shonks DeleteProfiles.vbs. Deletes old cached user profiles
- :: on computers listed in the names file or individually. Works with Vista.
- :: Requirements: 1. Run this script with a network admin account
- :: 2. Required files:
- :: - DeleteProfiles.vbs v1.9 or higher, specified in the PAYLOAD variable
- :: - psexec.exe
- :: These files may be in any of the following locations:
- :: a) the directory you run this script from
- :: b) in the PATH variable
- :: c) c:\windows\system32\
- :: 3. Put the names of your computers, one per line, in the NAMES_FILE in the same directory as this script
- :: Author: vocatus on reddit.com/r/sysadmin
- :: Joe Shonk, [email protected] -- DeleteProfiles.vbs
- :: History: 2.7 Cleaned up required files check into proper if statements
- :: 2.6 Added function to clear errorlevel in mass upload loop
- :: 2.5 Major code upgrade.
- :: - Logging function massively improved and debugged
- :: - Many glitches and failures fixed.
- :: 2.4 skipped
- :: 2.3 Some code cleanup and logging improvement.
- :: 2.2 Changes:
- :: - Added verbose flag to run-once portion
- :: - Improved log file rotation section significantly
- :: - Added ping -n 2 >NUL to the log collector loop, to prevent tripping McAfee
- :: 2.1 Changes:
- :: - Added log rotation code to auto-archive and age out log files
- :: - Added "PAYLOAD" variable to represent the DeleteProfiles.vbs script
- :: - Added check for existence of the .vbs script
- :: 2.0 Complete and major re-write, meant as a complete replacement for OCPN.bat
- :: - Now uses DeleteProfiles.vbs from Joe Shonk for more accuracy and compatibility
- :: - Fetches log files from remote computer after operation
- :: - Logs which registry keys deleted and why they were deleted
- :: - Can be invoked by specifying either a host or 'all' as the first argument, followed by
- :: the number of days. Example: OCPN2.bat all 30
- :: Prep
- SETLOCAL
- @echo off
- cls
- set VERSION=2.7
- title Orbital Cached Profile Nuker v%VERSION%
- :::::::::::::::
- :: VARIABLES :: -- set these
- :::::::::::::::
- :: Rules for variables:
- :: * NO quotes! (bad: "c:\directory\path" )
- :: * NO trailing slashes on the path! (bad: c:\directory\ )
- :: * Spaces are okay (okay: c:\my folder\with spaces )
- :: * Network paths are okay (okay: \\server\share name )
- :: ( \\172.16.1.5\share name )
- :: 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
- set NAMES_FILE=names.txt
- :: Exclude profiles whos name matches this string
- set EXCLUDE_PROFILES=admin*
- :: Connection timeout in seconds
- set TIMEOUT=2
- :: Name of the script used to do the deleting
- set PAYLOAD=DeleteProfiles.vbs
- :: Log location (directory)
- set LOCAL_LOGPATH=C:\Logs
- :: Note: setting these two variables has no effect, they get clobbered later.
- :: This is just to show you what they get set to.
- set REMOTE_LOGPATH=\\%TARGET%\C$\Logs
- set LOGFILE=%TARGET%_OCPN2.log
- :: Don't change anything below this line.
- :: If you you will break something and somewhere a puppy will die.
- set TARGET=%1
- set RUN_ONCE=false
- set DAYS=%2
- ::::::::::::::::::::::::::
- :: REQUIRED FILES CHECK ::
- ::::::::::::::::::::::::::
- :: Test if we're missing PAYLOAD file (the vbs script that does the work)
- IF NOT EXIST %PAYLOAD% (
- color 0c
- echo.
- echo ERROR:
- echo.
- echo Cannot find %PAYLOAD%. Place %PAYLOAD% in
- echo the same directory as this script to continue.
- echo.
- pause
- goto end
- )
- :: Test if we're missing PsExec
- IF EXIST psexec.exe goto run_test
- IF EXIST "%SystemDrive%\Program Files\SysInternalsSuite\psexec.exe" goto run_test
- IF EXIST %WINDIR%\system32\psexec.exe goto run_test
- color 0c
- echo.
- echo ERROR:
- echo.
- echo Cannot find PsExec.exe. Place PsExec.exe in
- echo the same directory as this script to continue.
- echo.
- pause
- goto end
- :: Test if we're doing a run-once
- :run_test
- IF '%1%'=='all' goto multiple_pc_run_once
- IF NOT '%1%'=='' goto single_pc_run_once
- cls
- :: User notice
- :intro
- color 0c
- echo.
- echo *********************************************************
- echo * *
- echo * ORBITAL CACHED PROFILE NUKER (OCPN) v%VERSION% *
- echo * ----------------------------------------------------- *
- echo * Nuke them from orbit. It's the only way to be sure. *
- echo * *
- echo * Windows 2000/XP/Vista caches user profiles at login. *
- echo * Over time these use up a lot of space. Annoying. *
- echo * *
- echo * This script deletes those old profiles. *
- echo * *
- echo *********************************************************
- echo.
- echo The next screen will let you set the number of days and
- echo the target.
- echo.
- pause
- cls
- :single_pc
- set DAYS=30
- color 07
- :: Ask user how many days old the profiles should be before getting nuked
- echo.
- echo *********************************************************
- echo * *
- echo * IT'S NUKING TIME *
- echo * ----------------------------------------------------- *
- echo * *
- echo * Rules for a safe and happy nuking: *
- echo * *
- echo * 1. Run this script with NETWORK ADMIN rights. Local *
- echo * admin rights won't work. *
- echo * 2. Run this script from the desktop, NOT a network *
- echo * path. *
- echo * *
- echo * After you enter the target and number of days and hit *
- echo * "enter" the script will begin nuking! *
- echo * *
- echo *********************************************************
- echo.
- :single_pc_loop
- title OCPN2 v%VERSION%
- echo.
- set /p TARGET=Enter IP, hostname or 'all':
- if %TARGET%==exit goto end
- set /P DAYS= Nuke profiles older than how many days? [%DAYS%]:
- if %DAYS%==exit goto end
- if %TARGET%==all goto multiple_pc_go
- set RUN_ONCE=false
- goto single_pc_go
- :: ========================================= ::
- :: SINGLE PC VERSION ::
- :: ========================================= ::
- :single_pc_run_once
- set RUN_ONCE=true
- if '%2%'=='' goto single_pc_run_once_need_days
- goto single_pc_go
- :single_pc_run_once_need_days
- set DAYS=30
- echo.
- echo Target: %TARGET%
- echo.
- set /P DAYS= Nuke profiles older than how many days? [%DAYS%]:
- if %DAYS%==exit goto end
- goto single_pc_go
- :single_pc_go
- title OCPN v%VERSION%: Nuking profiles, please wait...
- :: Set up our log information here.
- set REMOTE_LOGPATH=\\%TARGET%\C$\Logs
- set LOGFILE=%TARGET%_OCPN2.log
- cls
- echo.
- echo == Nuking cached profiles older than %DAYS% days on %TARGET%
- echo == Please wait... ==
- :: delay just so we can see what's going on
- ping localhost -n 2 >NUL
- echo.
- :: Steps
- :: 1. a. Make sure our Log directory exists and create it if it doesn't
- :: b. Upload file to remote computer
- :: 2. Call cscript.exe and execute the script on the remote computer. We display the verbose
- :: output to the console because we specifically requested this computer.
- :: For the multiple_pc version we don't display the output in the console window since it
- :: would be too noisy, but we DO create a log file and fetch it afterwards.
- :: 3. Fetch remote log file & dump it to the Logs directory on this computer
- :: 4. Display results
- echo.
- echo == Uploading script to host... ==
- ping %TARGET% -n 2 >NUL
- set ERRORLEVEL=0
- IF NOT EXIST %REMOTE_LOGPATH% mkdir %REMOTE_LOGPATH% >NUL
- copy %PAYLOAD% \\%TARGET%\C$ /Y >NUL
- echo.
- if %ERRORLEVEL%==1 echo == ERROR: Script failed to upload! ==
- if %ERRORLEVEL%==0 echo == Script uploaded successfully. ==
- echo.
- echo == Activating script on host... ==
- echo.
- :: Deletes profiles older than X days, and excludes accounts that match what we set earlier in "EXCLUDE_PROFILES."
- :: Since we specifically selected this computer, we log AND display output to console
- psexec -n %TIMEOUT% \\%TARGET% cscript //nologo C:\%PAYLOAD% /D %DAYS% /E %EXCLUDE_PROFILES% /V /C /L %REMOTE_LOGPATH%\%LOGFILE%
- del \\%TARGET%\C$\%PAYLOAD% /F /Q
- echo.
- echo == Fetching log file from host... ==
- set ERRORLEVEL=0
- copy %REMOTE_LOGPATH%\%LOGFILE% %LOCAL_LOGPATH% /Y >NUL
- if %ERRORLEVEL%==1 echo == ERROR: Couldn't fetch logfile! ==
- if %ERRORLEVEL%==0 echo == Fetched log successfully. ==
- echo.
- echo == OCPN Run Complete! ==
- echo.
- echo All profiles %DAYS% days or older were deleted from %TARGET%
- echo.
- echo Logfile is at: %LOCAL_LOGPATH%\%LOGFILE%
- echo Accounts with the text "%EXCLUDE_PROFILES%" in their names were excluded.
- echo.
- set TARGET=
- if %RUN_ONCE%==true goto end
- goto single_pc_loop
- REM ================================ REM
- REM MULTIPLE PC VERSION REM
- REM ================================ REM
- :multiple_pc_run_once
- set RUN_ONCE=true
- if '%2%'=='' goto multiple_pc_run_once_need_days
- goto multiple_pc_go
- :multiple_pc_run_once_need_days
- echo.
- echo Target: All computers listed in %NAMES_FILE%
- echo.
- set /P DAYS= Nuke profiles older than how many days? (30 recommended):
- if %DAYS%==exit goto end
- goto multiple_pc_go
- :multiple_pc_go
- title Nuking profiles, please wait...
- cls
- echo.
- echo LETS ROCK!!
- echo.
- echo Will delete cached profiles %DAYS% days and older on all computers
- echo listed in the "%NAMES_FILE%" file.
- echo.
- echo ======== Beginning OCPN2 mass run ========
- echo.
- :: Steps
- :: 1. For all machines in the names file:
- :: a. Ping the machine to make sure it's up
- :: b. Create C:\Logs directory if it doesn't exist
- :: c. Upload the %PAYLOAD% script to the computer
- :: 2. Call cscript.exe and execute the script on each remote computer.
- :: We start the script then disconnect and move on to the next system.
- :: The downside to this is we don't get to see console output; the upside
- :: is that it's much faster.
- :: 3. Wait 30 seconds
- :: 4. Go out and fetch log files
- :: 5. Compile log files and delete originals with a super-rad FOR loop
- :: 6. Results
- echo == Uploading to remote targets... ==
- echo.
- for /F %%i in (%NAMES_FILE%) do (
- ping %%i -n 1 >NUL
- set ERRORLEVEL=0
- IF NOT EXIST \\%%i\C$\Logs mkdir \\%%i\C$\Logs >NUL
- copy %PAYLOAD% \\%%i\C$ /Y >NUL
- if %ERRORLEVEL%==0 echo %%i ... OK
- if %ERRORLEVEL%==1 echo %%i ... FAILED
- set ERRORLEVEL=0
- )
- echo.
- echo == Done. ==
- echo.
- :: Once we've uploaded the script to all the remote hosts, we go back through
- :: the names file and activate it.
- ::
- :: Verbose logging here just in case something goes horribly wrong.
- echo.
- echo == Activating on targets... ==
- echo.
- for /F %%i in (%NAMES_FILE%) do (
- ping %%i -n 1 >NUL
- psexec -n %TIMEOUT% -d \\%%i cscript //nologo C:\%PAYLOAD% /D %DAYS% /V /E %EXCLUDE_PROFILES% /L \\%%i\C$\Logs\%COMPUTERNAME%_OCPN2.LOG
- echo.
- )
- echo.
- echo == Done. ==
- echo.
- echo == 2 min cooldown till log fetch... ==
- ping localhost -n 60 >NUL
- echo.
- echo == 1 min cooldown remaining... ==
- ping localhost -n 60 >NUL
- echo.
- echo == Cooldown done. ==
- echo.
- echo == Beginning log fetch... ==
- echo.
- :: Make a temp directory for compiling logs later
- mkdir %TEMP%\OCPN2 >NUL
- :: We have to use "*" in this loop because the log file is actually saved based on system name, not IP address.
- :: 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
- :: significantly more effort to parse out IP addresses remotely. It's easier to just grab any file ending in ocpn2.log.
- for /F %%i in (%NAMES_FILE%) do (
- ping %%i -n 1 >NUL
- set ERRORLEVEL=0
- copy \\%%i\C$\Logs\*ocpn2.log %TEMP%\OCPN2 /Y >NUL
- del \\%%i\C$\%PAYLOAD% /F /Q
- if %ERRORLEVEL%==0 echo %%i ... OK
- if %ERRORLEVEL%==1 echo %%i ... FAILED
- )
- echo.
- echo == Done. ==
- echo.
- echo == Rotating master log... ==
- :: Log file rotation. Archives up to 6 backups, ".log" through ".log5".
- :: Rotate & age out master logs, then create new blank log
- IF EXIST %LOCAL_LOGPATH%\OCPN2_master.log6 del %LOCAL_LOGPATH%\OCPN2_master.log6
- IF EXIST %LOCAL_LOGPATH%\OCPN2_master.log5 rename %LOCAL_LOGPATH%\OCPN2_master.log5 OCPN2_master.log6
- IF EXIST %LOCAL_LOGPATH%\OCPN2_master.log4 rename %LOCAL_LOGPATH%\OCPN2_master.log4 OCPN2_master.log5
- IF EXIST %LOCAL_LOGPATH%\OCPN2_master.log3 rename %LOCAL_LOGPATH%\OCPN2_master.log3 OCPN2_master.log4
- IF EXIST %LOCAL_LOGPATH%\OCPN2_master.log2 rename %LOCAL_LOGPATH%\OCPN2_master.log2 OCPN2_master.log3
- IF EXIST %LOCAL_LOGPATH%\OCPN2_master.log1 rename %LOCAL_LOGPATH%\OCPN2_master.log OCPN2_master.log2
- IF EXIST %LOCAL_LOGPATH%\OCPN2_master.log rename %LOCAL_LOGPATH%\OCPN2_master.log OCPN2_master.log1
- echo. > %LOCAL_LOGPATH%\OCPN2_master.log
- echo.
- echo == Done. ==
- echo.
- echo == Compiling log files... ==
- :: This loop compiles all the log files into a single master log.
- :: It's ugly but it seems to work.
- :: For each file in %TEMP%\OCPN2 of type (*), stick it in %%i and DO...this stuff
- FOR /r %TEMP%\OCPN2 %%i in (*) DO (
- echo. >> %LOCAL_LOGPATH%\OCPN2_master.log
- echo %%i >>%LOCAL_LOGPATH%\OCPN2_master.log
- echo ========================================================= >>%LOCAL_LOGPATH%\OCPN2_master.log
- type %%i >> %LOCAL_LOGPATH%\OCPN2_master.log
- )
- echo.
- echo == Done. ==
- echo.
- echo == Cleaning up... ==
- rmdir %TEMP%\OCPN2 /S /Q
- echo.
- echo == Done. ==
- echo.
- echo ========= OCPN mass run complete =========
- echo.
- echo All profiles %DAYS% days or older were deleted.
- echo Logfile: OCPN2_master.log
- echo.
- echo Accounts with the "%EXCLUDE_PROFILES%" in their name were excluded.
- echo.
- set TARGET=
- if %RUN_ONCE%==true goto end
- goto single_pc_loop
- :end
- ENDLOCAL
- title %USERNAME%
Add Comment
Please, Sign In to add comment