Clean Citrix Profile

http://www.jhouseconsulting.com/2008/07/30/script-to-replace-delprofexe-34


@echo off
REM This script will DELETE the user's local profile on all Citrix Servers, remove the
REM user's references from HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileList
REM and move the user's current Citrix Network profile to _OldProfiles\ on the main file server.
REM This Script should be run on the file server that hosts the user profiles.

REM ********** Required Configuration BEGIN *********
REM Set the OU in AD where all Citrix servers can be found
REM Eg: SET CITRIXOU=ou=Servers,ou=Citrix Server Group,DC=CONTOSO,DC=Local
SET CITRIXOU=

REM Set the Local path to the USER_FOLDERS folder.
REM Eg: SET USRFLDR=E:\USER_FOLDERS
SET USRFLDR=

REM Set the Local path to the USER_PROFILES folder.
REM Eg: SET USRPROF=E:\USER_PROFILES
SET USRPROF=

REM Set the Local path to the _OldProfiles folder. This is where your profiles will be backed up to.
REM Eg: SET OLDPROF=E:\_OldProfiles
SET OLDPROF=
REM ********** Required Configuration END *********
REM Clean up CitrixServer list.
if exist citrixservers.txt del /q citrixservers.txt

for /f "tokens=2,3,4 delims=/ " %%a in ("%date%") do (
 set day=%%b
 set month=%%a
 set year=%%c
)
for /f "tokens=1,2 delims=: " %%g in ("%time%") do (
 set hour=%%g
 set minute=%%h
)

Echo.
Echo.

REM Prompting for the username

:input
Echo.
set INPUT=
set /P INPUT=Enter the username that you would like to clean: %=%
Echo.
if "%INPUT%"=="" Echo No Username Entered. Please enter a username & goto input
REM Setting Variables that use the username

if not exist Logs\%INPUT% mkdir Logs\%INPUT%
SET LOG=Logs\%INPUT%\%MONTH%-%DAY%-%YEAR%.%hour%-%minute%.log
set OLDPROF=%OLDPROF%\%INPUT%\%month%-%DAY%-%year%.%hour%-%minute%\
Echo.
Echo The script will clean %INPUT%'s user profile.
Echo %DATE% %TIME% Cleaning %INPUT%'s Profile >> %LOG%
Echo.
CHOICE /C YN /M "Press Y to continue, N to cancel."
if %ERRORLEVEL%==2 goto end

 

for /F %%i in ('dsquery * -filter "&(objectcategory=user)(samaccountname=%INPUT%)" -attr objectsid ^| find /v "objectsid"') do set USERSID=%%i
cls
Echo.
Echo %INPUT%'s SID is = %USERSID%
Echo %DATE% %TIME% %INPUT%'s SID is = %USERSID% >> %LOG%
Echo.
Echo Getting List of Citrix Servers... This may take a minute.
for /f "Tokens=1 delims=$" %%i in ('dsquery computer "%CITRIXOU%" ^| dsget computer -samid^|find /V "samid" ^| find /V "dsget"') do echo %%i >> CitrixServers.txt
Echo.
Echo The list of active Citrix servers is:
Echo %DATE% %TIME% The list of active Citrix servers is: >> %LOG%
Type CitrixServers.txt
Type CitrixServers.txt >> %LOG%
Echo.
pause

 
cls
Echo.
Echo Do you want to go ahead with the Cleanup of %INPUT%'s userprofile?
Echo.
Echo.
Echo The following actions will be performed:
Echo 1. Delete %INPUT%'s local profile on all active Citrix servers
Echo 2. Delete the user profile path registry key on all Citrix servers
Echo 3. Move %USRFLDR% to %OLDPROF%\%INPUT%.%USERDOMAIN%
Echo 4. Move %USRPROF% to %OLDPROF%\%INPUT%.%USERDOMAIN%.V2
Echo.
CHOICE /C YN /M "Proceed? Press Y to continue, N to cancel."
if %ERRORLEVEL%==2 goto end

 

cls
Echo.
Echo Starting Cleanup... Please Wait...
for /F %%i in (citrixservers.txt) do (
Echo Cleaning up registry for user %INPUT% on %%i
Echo %DATE% %TIME% Cleaning up registry for user %INPUT% on %%i >> %LOG%
reg delete "\\%%i\hklm\software\microsoft\windows nt\currentversion\profilelist\%USERSID%" /f >> %LOG%
Echo.
Echo Taking ownership of %INPUT%'s profile on %%i
Echo %DATE% %TIME% Taking ownership of %INPUT%'s profile on %%i >> %LOG%
takeown /F \\%%i\c$\users\%INPUT% /A /R /D y >> %LOG%
Echo Taking ownership of %INPUT%'s profile on %%i complete.
Echo Cleaning up %INPUT%'s profile on %%i
Echo %DATE% %TIME% Cleaning up %INPUT%'s profile on %%i >> %LOG%
rmdir /s /q \\%%i\c$\users\%INPUT%
Echo Cleaning up %INPUT%'s profile on %%i complete.
Echo.
Echo.
)
REM Checking to see if the destination for backups exist and creating them as necessary

if not exist %OLDPROF% mkdir %OLDPROF%

 

REM Processing USER_FOLDER

SET USRFLDR=%USRFLDR%\%INPUT%.%USERDOMAIN%

Echo Taking ownership and assigning permissions on %USRFLDR%
Echo %DATE% %TIME% Taking ownership of %USRFLDR% >> %LOG%

takeown /F %USRFLDR% /A /R /D y >> %LOG%
icacls %USRFLDR% /grant administrators:f /t >> %LOG%

Echo Moving %USRFLDR% to %OLDPROF%
Echo %DATE% %TIME% Moving %USRFLDR% to %OLDPROF% >> %LOG%

move /y %USRFLDR% %OLDPROF% >> %LOG%

 
REM Processing USER_PROFILES

SET USRPROF=%USRPROF%\%INPUT%.%USERDOMAIN%.V2

Echo Taking ownership and assigning permissions on %USRPROF%
Echo %DATE% %TIME% Taking ownership of %USRPROF% >> %LOG%

takeown /F %USRPROF% /A /R /D y >> %LOG%
icacls %USRPROF% /grant administrators:f /t >> %LOG%

Echo Moving %USRPROF% to %OLDPROF%
Echo %DATE% %TIME% Moving %USRPROF% to %OLDPROF% >> %LOG%

move /y %USRPROF% %OLDPROF%

cls
Echo.
Echo.
Echo The following has been completed:
Echo 1. Deleted %INPUT%'s local profile on all active Citrix servers
Echo 2. Deleted the user profile path registry key on all Citrix servers
Echo 3. Moved %USRFLDR% to %OLDPROF%\%INPUT%.%USERDOMAIN%
Echo 4. Moved %USRPROF% to %OLDPROF%\%INPUT%.%USERDOMAIN%.V2
:end

SET INPUT=
SET USERSID=
SET ERRORLEVEL=
SET USRFLDR=
SET USRPROF=
SET OLDPROF=
pause
start notepad %LOG%

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s