Bad syntax in Batch File - batch-file

It is very late, and I'm tired at looking at this. Could someone explain where I might have bad syntax in this script?
The script looks to see if an old installation exists on a live machine within computers.txt file. If so, it should uninstall it, copy over the new installation, and then install it. If anything fails, log to its respective log file.
#echo off
:CheckifLogsExist
if NOT exist Uninstall.log (
copy /y nul Uninstall.log
) else (
del Uninstall.log && copy /y nul Uninstall.log
)
if NOT exist WMIC.log (
copy /y nul WMIC.log
) else (
del WMIC.log && copy /y nul WMIC.log
)
if NOT exist Copying.log (
copy /y nul Copying.log
) else (
del Copying.log && copy /y nul Copying.log
)
if NOT exist Install.log (
copy /y nul Install.log
) else (
del Install.log && copy /y nul Install.log
)
:checkifalive
for /F %%I IN (computers.txt) DO
(
ping -n 1 %%I
if NOT %errorlevel%==0 echo Machine offline && goto:EOF
:Uninstall
echo "Uninstalling previous version of Symantec Endpoint Protection"
psexec \\%%I -s wmic failfast:on product where name="Symantec Endpoint Protection" call uninstall /nointeractive
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> Uninstall.log
:copy
echo "Finding out which processor is in the machine"
wmic cpu list brief > temp.out
findstr /I "86" temp.out && goto copy86 || goto copy64
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> WMIC.log
:copy86
echo "Copying the installation to the local machine"
copy "C:\installation.exe" \\%%I
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> Copying.log
goto Install86
:copy64
echo "Copying the installation to the local machine"
copy "C:\installation.exe" \\%%I
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> Copying.log
goto Install64
:Install86
echo "Installing upgraded Symantec Endpoint Protection"
psexec \\%%I -s "C:\installation.exe /s"
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> Install.log
goto Finish
:Install64
echo "Installing upgraded Symantec Endpoint Protection"
psexec \\%%I -s "C:\installation.exe /s"
if NOT %errorlevel%==0 echo %%I - %errorlevel% >> Install.log
goto Finish
:Finish
)

Your syntax error is that these need to be on the same line, with a space after do
Note Stephan's comment that you also need to enable delayed expansion or as Vicky says use a subroutine that the forindo command will call
for /F %%I IN (computers.txt) DO
(

#echo off
:clear_logfiles :: Label not needed
:: copy command doesn't care, if the file exists or not, it just (re)creates it with size 0:
copy /y nul Uninstall.log
copy /y nul WMIC.log
copy /y nul Copying.log
copy /y nul Install.log
:checkifalive :: Label not needed
for /F %%I IN (computers.txt) DO ( call DoIt %%i )
echo all done.
goto :eof
REM this is the subroutine
:DoIt
ping -n 1 %1
if NOT %errorlevel%==0 (
echo Machine offline
goto :EOF
)
REM Uninstall
echo "Uninstalling previous version of Symantec Endpoint Protection"
psexec \\%1 -s wmic failfast:on product where name="Symantec Endpoint Protection" call uninstall /nointeractive
if NOT %errorlevel%==0 echo %1 - %errorlevel% >> Uninstall.log
REM copy
echo "Finding out which processor is in the machine"
wmic cpu list brief | findstr /I "86"
:: why checking for 86/64 if the code is exactly the same for both? Anyway - here we go:
if %errorlevel%==0 ( call copy86 ) else ( call copy64 )
goto :eof
:copy86
echo "Copying the installation to the local machine"
copy "C:\installation.exe" \\%1
if NOT %errorlevel%==0 (
echo %1 - %errorlevel% >> Copying.log
) else (
REM Install86
echo "Installing upgraded Symantec Endpoint Protection"
psexec \\%1 -s "C:\installation.exe /s"
if NOT %errorlevel%==0 echo %1 - %errorlevel% >> Install.log
)
goto :eof
:copy64
echo "Copying the installation to the local machine"
copy "C:\installation.exe" \\%1
if NOT %errorlevel%==0(
echo %1 - %errorlevel% >> Copying.log
) else (
REM Install64
echo "Installing upgraded Symantec Endpoint Protection"
psexec \\%1 -s "C:\installation.exe /s"
if NOT %errorlevel%==0 echo %1 - %errorlevel% >> Install.log
)
goto :eof

Related

error loading text as variable from file and check it

this is some code it suppose to check for adb.exe in a location then if it exist will proceed to code if not will ask user to input location manually and check again if location is right will save the input in file "dontremoveoredit" to use it next time user open the bat file
the problem is if the file is empty or contain path it will crash the bat and close
:: #echo off
If exist "C:\Program Files\ui\adb.exe" (
goto yes
) else (
goto adbexist
)
:yes
set PATH=%PATH%;C:\Program Files\ui"
:: #cd/d "%~dp0"
adb.exe kill-server
adb.exe devices
adb connect localhost:5037
Echo.
Echo.
Echo.
echo Yes Was Executed
Pause>nul
Exit
:no
msg * "Couldn't find The adb.exe in Default Bath"
ping localhost -n 3 >Nul
goto noadbtext
:adbexist
If exist dontremoveoredit (
goto adbexist1
) else (
goto no
)
:adbexist1
set /p var=<dontremoveoredit
if [%var%] == [] (
goto noadbtext
) else (
goto lala
)
:lala
If exist %var%\adb.exe (
set PATH=%PATH%;%var%"
adb.exe kill-server
adb.exe devices
adb connect localhost:5037
goto dns
) else (
msg * "Make Sure You Entered The Right Path To UI"
goto noadbtext
)
:noadbtext
break>"dontremoveoredit"
set /p EmulatorUIBath=Enter Your Emulator's UI folder Path :
If exist "%EmulatorUIBath%\adb.exe" (
#echo %EmulatorUIBath%>> "dontremoveoredit"
goto adbexist2
) else (
msg * "Make Sure You Entered The Right Path To UI"
goto noadbtext
)
:adbexist2
set PATH=%PATH%;%EmulatorUIBath%"
adb.exe kill-server
adb.exe devices
adb connect localhost:5037
echo adb Exist2
Pause>nul
exit
Here you are my laptop is about to die but this should do the needful nicely. :)
Slightly Updated Version I mentioned I woudl post.
#(
SETLOCAL EnableDelayedExpansion
ECHO OFF
SET "_ConfigFilePath=%~0dpn_DO_NOT_REMOVE.dat"
SET "_ADBPath="
SET "_eLvl=0"
SET "ECHO_RW=Call :Echo_Color CF "
SET "ECHO_GB=Call :Echo_Color 20 "
SET "ECHO_AB=Call :Echo_Color B0 "
SET "ECHO_LY=Call :Echo_Color 1E "
SET "ECHO_YL=Call :Echo_Color E1 "
SET "ECHO_YR=Call :Echo_Color EC "
SET "ECHO_RY=Call :Echo_Color CE "
SET "ECHO_BY=Call :Echo_Color 1E "
)
CALL :Main
(
ECHO. Script Completed With ErrorLevel of %_eLvl%
ENDLOCAL
Exit /B %_eLvl%
)
:Echo_Color
ECHO.>"%~2"
FINDStr /A:%~1 /I ".*" "%~2" NUL
DEL /F /Q "%~2" >nul 2>nul
GOTO :EOF
:Main
REM Checing for ADB
CALL :CheckADB
IF /I %_eLvl% NEQ 0 (
ECHO. Error Encountered, Exiting!
) ELSE (
CALL :RunADB
)
PAUSE
GOTO :EOF
:CheckADB
REM Check For ADB Installed in Program Directories
FOR /F "Tokens=1* Delims==" %%A IN ('
SET ProgramFiles
') DO (
FOR /F "tokens=*" %%a IN ('
WHERE /R "%%A" /F "adb.exe" 2^>nul
') DO (
ECHO Found ADB here: "%%~a"
SET "_ADBPath=%%~a"
)
)
REM ECHO.Finished Where Loop
IF NOT DEFINED _ADBPath (
ECHO. ADB Install not Found, checking Config file.
IF EXIST "!_ConfigFilePath!" (
ECHO. Config File Exists checking contents..
FOR /F "Tokens=*" %%A IN ('
TYPE "!_ConfigFilePath!"
') DO (
ECHO. Saved ADB Path Found: "%%~A" in Config file.
IF EXIST "%%~A" (
SET "_ADBPath=%%~A"
) ELSE (
ECHO. Saved ADB Path "%%~A" Does Not Exist! Deleteing Config File.
CALL :Get_ADB_Path_From_User
)
)
) ELSE (
CALL :Get_ADB_Path_From_User
)
)
GOTO :EOF
:Get_ADB_Path_From_User
CLS
REM COLOR CF
ECHO.
COLOR 1E
FOR %%A IN (
"==========================================================="
"= ADB was not detected on your system ="
"==========================================================="
) DO (
REM ECHO.>"%%~A"
REM FINDStr /A:CF /I ".*" "%%~A" NUL
REM DEL /F /Q "%%~A" >nul 2>nul
%ECHO_RY% "%%~A"
)
ECHO.
ECHO.
%ECHO_AB% "Please enter a custom path to ADB below."
ECHO.
ECHO.
SET /P "_TmpADBPath=What Is the Path to your ADB Install? "
CLS
IF NOT EXIST "%_TmpADBPath%" (
%ECHO_RW% "==========================================================="
%ECHO_RW% "== =="
%ECHO_RW% "== Unable to Verify that The path Provided Exists =="
%ECHO_RW% "== =="
%ECHO_RW% "==========================================================="
SET "_eLvl=1"
GOTO :EOF
) ELSE (
COLOR 2F
SET "_ADBPath=%_TmpADBPath%"
ECHO.===========================================================
ECHO. Path Exists:
ECHO.
ECHO. "%_TmpADBPath%"
ECHO.
ECHO. ===========================================================
ECHO.
ECHO.
CHOICE /M "Save this Path for future Use?"
IF /I "%ERRORLEVEL%" EQU "0" (
ECHO.%_TmpADBPath%>"%_ConfigFilePath%"
ECHO.
ECHO. Saved to: "%_ConfigFilePath%"
) ELSE (
ECHO.
ECHO. Okay, Will only use it temporarily.
)
COLOR
)
GOTO :EOF
:RunADB
"%_ADBPath%" kill-server
"%_ADBPath%" devices
"%_ADBPath%" connect localhost:5037
Echo.
Echo.
Echo.
Echo.Yes Was Executed
Pause>nul
Exit

FOR Loop will not run.

I have been having the hardest time with this FOR loop. I thought it was working the other day, but when I went to use it today, it didn't. This is part of a much larger script, but this is the only portion that is hanging me up. Also, to note, I did not write the majority of the script. It was given to me, and I have had to modify it to fit my needs. Can someone take a look and let me what I am missing.
IF NOT EXIST C:\Paytronix\config\deploytool\terminals.xml (
copy terminals.xml_template terminals.xml > temp.txt
set MAKETERMXML=1
) ELSE (
set MAKETERMXML=0
)
echo Create terminals.xml = %MAKETERMXML%
PING localhost -n 5 >NUL
for /l %%T in (1,1,%TERMINALS%) do (
echo Trying terminal %TERMSTRING%%%T
PING localhost -n 5 >NUL
REM net use /user:USERNAME /PERSISTENT:NO P: \\%TERMSTRING%%%T\C$ PASSWORD
net use P: \\%TERMSTRING%%%T\C$ /USER:%USR% %PWRD% /PERSISTENT:NO > temp.txt
REM /USER:username password (Insert after C$
REM net use P: \\%TERMSTRING%%%T\C$ /PERSISTENT:NO > temp.txt
setlocal EnableDelayedExpansion
echo Copying Installer Shortcut to Startup Folder
IF EXIST "\\%TERMSTRING%%%T\C$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup" (
copy aaaPxAlohaUiInstaller.lnk "\\%TERMSTRING%%%T\C$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup" > result.txt
) ELSE (
copy aaaPxAlohaUiInstaller.lnk "\\%TERMSTRING%%%T\C$\Documents and Settings\All Users\Start Menu\Programs\Startup" > result.txt
)
PING localhost -n 5 >NUL
find /c "1" result.txt > copyresult.txt
FOR /F "tokens=1,2,3" %%a in (copyresult.txt) do set COPYTAG=%%c
echo Copy Result = !COPYTAG!
PING localhost -n 5 >NUL
IF !COPYTAG!==1 (
echo SUCCESS !!! Valid Terminal %TERMSTRING%%%T > C:\Paytronix\Paytronix.bd
echo SUCCESS !!! Valid Terminal %TERMSTRING%%%T
echo Updating PxInst folder
IF NOT EXIST "\\%TERMSTRING%%%T\C$\PxInst\" mkdir \\%TERMSTRING%%%T\C$\PxInst > temp.txt
REM Delete PxInst contents
del /F /S /Q "\\%TERMSTRING%%%T\C$\PxInst\*" > temp.txt
REM Create PxAlohaUiInstaller folder
IF NOT EXIST "\\%TERMSTRING%%%T\C$\PxInst\PxAlohaUiInstaller" mkdir \\%TERMSTRING%%%T\C$\PxInst\PxAlohaUiInstaller > temp.txt
REM Update user program installer
copy /Y C:\Paytronix\PxAlohaUiInstaller\PxAlohaUiInstaller.exe \\%TERMSTRING%%%T\C$\PxInst\PxAlohaUiInstaller > temp.txt
REM Copy PxAlohaUiInstaller done
PING localhost -n 5 >NUL
REM Create user program dir if missing
IF NOT EXIST "\\%TERMSTRING%%%T\C$\Paytronix" (
echo Creating User Program folder
mkdir \\%TERMSTRING%%%T\C$\Paytronix\config\userprog > temp.txt
copy C:\Paytronix\config\userprog\pxalohaui.cfg \\%TERMSTRING%%%T\C$\Paytronix\config\userprog\ > temp.txt
)
::echo
IF %MAKETERMXML%==1 (
echo ^<Terminal^> >> terminals.xml
echo ^<Name^>term%%T^</Name^> >> terminals.xml
echo ^<Path^>\\%TERMSTRING%%%T\C$^</Path^> >> terminals.xml
echo ^<Address^>%TERMSTRING%%%T^</Address^> >> terminals.xml
echo ^</Terminal^> >> terminals.xml
)
)
endlocal
net use P: /DELETE /YES > temp.txt
del result.txt
del copyresult.txt
del temp.txt
)
echo %MAKETERMXML%
IF %MAKETERMXML%==1 (
echo ^</TerminalList^> >> terminals.xml
echo ^</PaytronixConfig^> >> terminals.xml
copy terminals.xml C:\Paytronix\config\deploytool\terminals.xml
)

How to force exit command line bat file when occur download error

Dears
Here is my .bat command line
I use bitsadmin /transfer command download from url.
but got error(like disconnect network...etc) I need continue execute remain command.
But now I can't get achievement... what should I do
#echo off
:: Filter updater for HCK and HLK
:::::::::::::::::::::::::: Settings :::::::::::::::::::::::::::::::::
:: Notice: As of July 2015, the HCK and the HLK filter updates are the exact same file, downloaded from the same location!
SET "source=https://sysdev.microsoft.com/member/SubmissionWizard/LegalExemptions/HCKFilterUpdates.cab"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SET "destination=C:\FilterUpdates.cab"
if not exist "%DTMBIN%\" (
echo ERROR: folder "%DTMBIN%"
echo does not exist! Please verify that you have the controller installed.
pause
exit /B 1
)
echo Please make sure that all instances of the Studio are turned OFF!
echo Downloading Filters...
bitsadmin /transfer "Downloading Filters" "%source%" "%destination%"
if errorlevel 1 goto end
echo Extracting...
expand -i "%destination%" -f:UpdateFilters.sql "%DTMBIN%\"
if not errorlevel 0 echo ERROR & exit /B 1
echo Installing...
pushd "%DTMBIN%\"
if not errorlevel 0 echo ERROR & exit /B 1
"%DTMBIN%\updatefilters.exe " /s
if not errorlevel 0 echo ERROR & exit /B 1
popd
:end
exit
Your problem could be caused by the errorlevels generated by bitsadmin: some of them are negative values and the test if errorlevel 1 will be evaluated as false (if errorlevel n is true for values greater than or equal to n)
You will need to read and test the value of the errorlevel variable
if not %errorlevel%==0 exit /b 1
But sometimes, bitsadmin will have errors and will generate a errorlevel 0, so, you need to manualy check the status
#echo off
setlocal enableextensions disabledelayedexpansion
:: Filter updater for HCK and HLK
:::::::::::::::::::::::::: Settings :::::::::::::::::::::::::::::::::
:: Notice: As of July 2015, the HCK and the HLK filter updates
:: are the exact same file, downloaded from the same location!
SET "source=https://sysdev.microsoft.com/member/SubmissionWizard/LegalExemptions/HCKFilterUpdates.cab"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
if not exist "%DTMBIN%\" (
echo ERROR: folder "%DTMBIN%"
echo does not exist! Please verify that you have the controller installed.
pause
exit /B 1
)
SET "destination=C:\FilterUpdates.cab"
if exist "%destination%" del /q "%destination%"
echo Please make sure that all instances of the Studio are turned OFF!
echo Creating download task...
set "taskName=[HCK_FilterUpdater]"
>nul (
rem remove task if already present
bitsadmin /list | find "%taskName%" && bitsadmin /cancel "%taskName%"
rem create the task
bitsadmin /create "%taskName%"
rem include our file in the task
bitsadmin /addfile "%taskName%" "%source%" "%destination%"
rem start the download
bitsadmin /resume "%taskName%"
)
echo Downloading...
set "exitCode="
for /l %%a in (1 1 500) do if not defined exitCode for /f "delims=" %%a in ('
bitsadmin /info "%taskName%"
^| findstr /b /l /c:"{"
') do for /f "tokens=3,*" %%b in ("%%a") do (
if "%%~b"=="TRANSFERRED" (
set "exitCode=0"
>nul bitsadmin /complete "%taskName%"
echo ... done
)
if "%%~b"=="ERROR" (
set "exitCode=1"
bitsadmin /geterror "%taskName%" | findstr /b /c:"ERROR"
>nul bitsadmin /cancel "%taskName%"
)
if not defined exitCode (
echo(%%b %%c
timeout /t 2 >nul
)
)
if not defined exitCode ( echo TIMEOUT & exit /b 1 )
if not exist "%destination%" ( echo ERROR & exit /b 1 )
echo Expanding...
>nul expand -i "%destination%" -f:UpdateFilters.sql "%DTMBIN%"
if errorlevel 1 ( echo ERROR & exit /b 1 )
echo Installing...
pushd "%DTMBIN%" || ( echo ERROR & exit /b 1 )
".\updatefilters.exe " /s || ( echo ERROR & exit /b 1 )
popd
It seems like once command shell is in bitsadmin mode you can't take it out of it. My solution unfortunately is two seperate .bats.

How to do while loop in windows batch

I wrote a windows batch script to check and move files to another directory based on the list I put in a notepad file named list.txt. But I have no idea that how to set the while-loop. Only to jump out of the subroute when the condition fulfill.
In C Programming, we could write like this by setting a while-loop direcly. But seems in windows batch is quite different.
All I want is like this:
Directory A:
1. A.txt
2. B.txt
3. list.txt (By line sequential with filename want to move)
4. process.bat
Directory B:
None of files (Then move a file by order of line set in list.txt) OR
A.txt (If already existed a text file in directory, process.bat will pause before A.txt disappear)
Process.bat
#echo off
:readline
for /f "tokens=*" %%a in (list.txt) do call :processline %%a
goto :eof
:processline
if exist D:\DirectoryA\*.txt (
echo %time% >> D:\AutoLog\Log.txt
echo Previous job did not finished yet. >> D:\AutoLog\Log.txt
Timeout /t 30
echo.
)
set name=%*
if exist %name%.txt (
echo %time% >> D:\AutoLog\Log.txt
echo File found and processing %name%.txt now... >> D:\AutoLog\Log.txt
copy "%~dp0\%name%.txt" "D:\DirectoryB"
echo Transfer %name%.txt completed!! >> D:\AutoLog\Log.txt
echo. >> D:\AutoLog\Log.txt
Timeout /t 790
echo.
echo ==============================================================
)
:eof
Please guide me to finish the script by using a while-loop method. Thanks
I change some script sequence and it works now.
#echo off
:readline
for /f "tokens=*" %%a in (list.txt) do call :processline %%a
goto :eof
:processline
set name=%*
if exist C:\Test2\*.txt (
echo %date% %time% >> C:\Test2\Log.txt
echo Previous job did not finished yet. >> C:\Test2\Log.txt
Timeout /t 5
echo.
echo. >> C:\Test2\Log.txt
goto :processline
)
if exist %name%.txt (
echo %date% %time% >> C:\Test2\Log.txt
echo File found and processing %name%.txt now... >> C:\Test2\Log.txt
copy "%~dp0\%name%.txt" "C:\Test2"
echo Transfer %name%.txt completed!! >> C:\Test2\Log.txt
echo. >> C:\Test2\Log.txt
Timeout /t 10
echo.
echo ==============================================================
)
:eof
This will copy as well as count the number of lines from your text file..
# echo off
:TextPath
cls
set /p Input=#1 Enter the full path of the text file :
set /p Source=#2 Enter the full path of Source :
set /p Target=#3 Enter the full path of Destination :
:choice
set /P c=Ready to Continue[Y/N]?
if /I "%c%" EQU "Y" goto :Yes
if /I "%c%" EQU "N" goto :No
goto :choice
:Yes_Local
for /f "delims=" %%i in (%Input%) do echo f| xcopy /f /y "%Source%\%%i" "%Target%\%%i"
for /f %%C in ('Find /V /C "" ^< %Input%') do set Count=%%C
msg * No of Lines executed= %Count%
exit
:No
cls
color e
echo Redirecting to Main....
PING 127.0.0.1 -n 2 >NUL
cls
echo Please wait
PING 127.0.0.1 -n 4 >NUL
goto TextPath

Using native Win 7 batch methods to remotely recreate a users profile?

I made a simple batch for XP that our team used to recreate XP profiles in a corporate environment but we have since moved to Win 7. Obviously recreating a User Profile in 7 is a little bit more complicated than simply renaming the old profile, dropping a Restore batch in the All Users startup folder and then letting Windows create a fresh profile on login. Writing a batch tends to get a little too complicated on the if/then/else statements for me, so I'm hoping to keep it simple like the old one. Someone suggested I incorporate a small command-line friendly user profile deletion utility into the batch, but I haven't found any that first offers a backup. Which means I'd have to Robocopy the entire user profile first, then restore all the data on login. I'm "hoping" to avoid that time investment. It also makes me a bit nervous relying on no errors during the copying process before being all deleted on the next step. Which is why I really like the renaming of user profiles in XP for worst case scenarios. Can anyone point me in the direction of either a simple utility that does this, or can throw some quick and easy code together I might be able to use? Most of my XP batch was just using c$ access, simple commands like
rename "\\%ip%\c$\Documents and Settings\%USERNAME%" %USERNAME%.%date%
All your help is greatly appreciated!
The path has changed in Win 7. It's now c:\users.
if exist c:\user rename "\\%ip%\c$\Users\%USERNAME%" "%USERNAME%.%newdate%"
if exist "c:\Documents and settings" rename "\\%ip%\c$\Documents and Settings\%USERNAME%" "%USERNAME%.%newdate%"
Also %date% will contain illegal characters .
Set newdate=%date:\=%
Will put the digits without seperators into newdate.
We utilize this in an Win 7 environment and does not have to be ran with elevated rights and can be done remotely. Key is the PC has be restarted and the user to not log in until after this script has run. After the user logs in and the script completes on the remote PC. You will have to remap any network printers and attach any PST files in Outlook.
#echo off
cls
title Profile Rebuilder (Local/Remote)
echo.
echo.
echo This batch file will rebuild a user's Windows 7 profile.
echo It is intended to help speed the process of rebuilding
echo a corrupted user profile either locally (while logged in
echo as DFASADMIN), or remotely. Either way the user who's
echo profile is being rebuilt can't be logged in and the PC
echo must have been rebooted.
echo.
echo.
set /p cont=Do you wish to continue with the profile rebuild (y/n):
if /i "%cont%"=="n" goto end
:local
echo.
set /p local=Is this script being run from a remote PC (y/n):
:rebooted
echo.
set /p reb=Has the PC been rebooted (y/n):
if /i "%reb%"=="y" goto renameremote
if /i "%local%"=="y" goto warn2
:warn1
cls
color c0
echo.
echo This script can not continue until the PC has been rebooted!
echo Please reboot now and then re-run this batch file.
echo.
pause
exit
:warn2
cls
color c0
echo.
echo This script can not continue until the PC has been rebooted!
echo Please have the user reboot now. Ensure they do not login.
echo.
pause
color 0f
goto rebooted
:renameremote
if /i "%local%"=="n" goto renamelocal
cls
echo.
echo.
set /p comp=Please enter the Computer Name or IP address of the remote PC:
echo.
echo Available Profiles:
dir /b "\\%comp%\c$\Users" | Find /v /i "users" | Find /v /i "default" | Find /v /i "dfasadmin" | Find /v /i "Public" | Find /v /i "runapps" | Find /v /i "mrbaInstall" | Find /v /i "test"
If Not %ErrorLevel%==0 goto Error
goto vername
:Error
cls
color c0
echo.
echo %comp% could not be contacted.
echo Please verify network connectivity and try again.
echo.
echo.
pause
exit
:vername
echo.
set /p name=Enter the profile name (i.e. firstname_lastname):
if /i not exist "\\%comp%\c$\Users\%name%" goto vername
if exist "\\%comp%\c$\Users\%name%.bak2" move "\\%comp%\c$\Users\%name%.bak2" "\\%comp%\c$\Users\%name%.bak3"
if exist "\\%comp%\c$\Users\%name%.bak" move "\\%comp%\c$\Users\%name%.bak" "\\%comp%\c$\Users\%name%.bak2"
move "\\%comp%\c$\Users\%name%" "\\%comp%\c$\Users\%name%.bak"
if /i not exist "\\%comp%\c$\Users\%name%.bak" goto failed
set drv=\\%comp%\c$
goto restore
:renamelocal
cls
echo.
echo Available Profiles:
dir /b "C:\Users" | Find /v /i "user" | Find /v /i "default" | Find /v /i "dfasadmin" | Find /v /i "helpdesk" | Find /v /i "runapps" | Find /v /i "mrbaInstall" | Find /v /i "test"
:vername2
echo.
set /p name=Enter the profile name (i.e. firstname_lastname):
if /i not exist "C:\Users\%name%" goto vername2
if exist "C:\Users\%name%.bak2" move "C:\Users\%name%.bak2" "C:\Users\%name%.bak3"
if exist "C:\Users\%name%.bak" move "C:\Users\%name%.bak" "C:\Users\%name%.bak2"
move "C:\Users\%name%" "C:\Users\%name%.bak"
if /i not exist "C:\Users\%name%.bak" goto failed
set drv=C:
:restore
echo #echo off >"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo cls >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo title Restoring Profile Data >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo. >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo **************************************************** >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo. >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo DO NOT CLOSE THIS WINDOW UNLESS INSTRUCTED TO DO SO. >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo. >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo **************************************************** >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo. >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo. >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo Restoring The User's Desktop... >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo xcopy "C:\Users\%name%.bak\Desktop" "C:\Users\%name%\Desktop" /c /g /h /i /k /q /s /y /exclude:C:\rbexclude.txt >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo Restoring The User's Favorites... >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo xcopy "C:\Users\%name%.bak\Favorites" "C:\Users\%name%\Favorites" /c /g /h /i /k /q /s /y /exclude:C:\rbexclude.txt >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo Restoring The User's Documents... >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo xcopy "C:\Users\%name%.bak\Documents" "C:\Users\%name%\Documents" /c /g /h /i /k /q /s /y /exclude:C:\rbexclude.txt >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo xcopy "C:\Users\%name%.bak\My Documents" "C:\Users\%name%\My Documents" /c /g /h /i /k /q /s /y /exclude:C:\rbexclude.txt >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo Restoring The User's Pictures... >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo xcopy "C:\Users\%name%.bak\Pictures" "C:\Users\%name%\Pictures" /c /g /h /i /k /q /s /y /exclude:C:\rbexclude.txt >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo xcopy "C:\Users\%name%.bak\My Pictures" "C:\Users\%name%\My Pictures" /c /g /h /i /k /q /s /y /exclude:C:\rbexclude.txt >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo Restoring The User's PST files... >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo if not exist "C:\Users\%name%\Documents\Outlook Files" mkdir "C:\Users\%name%\Documents\Outlook Files"
echo if not exist "C:\Users\%name%\My Documents\Outlook Files" mkdir "C:\Users\%name%\My Documents\Outlook Files"
echo xcopy "C:\Users\%name%.bak\AppData\Local\Microsoft\Outlook\*.pst" "C:\Users\%name%\Documents\Outlook Files" /c /g /h /i /k /q /s /y /exclude:C:\rbexclude.txt >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo xcopy "C:\Users\%name%.bak\AppData\Local\Microsoft\Outlook\*.pst" "C:\Users\%name%\My Documents\Outlook Files" /c /g /h /i /k /q /s /y /exclude:C:\rbexclude.txt >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo Restoring The User's Roaming Settings... >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo xcopy "C:\Users\%name%.bak\AppData\Roaming\Microsoft\Signatures" "C:\Users\%name%\AppData\Microsoft\Signatures" /c /g /h /i /k /q /s /y /exclude:C:\rbexclude.txt >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo xcopy "C:\Users\%name%.bak\AppData\Roaming\Microsoft\Proof" "C:\Users\%name%\AppData\Microsoft\Proof" /c /g /h /i /k /q /s /y /exclude:C:\rbexclude.txt >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo if exist "C:\Users\%name%.bak\WinGamps32.ini" xcopy "C:\Users\%name%.bak\WinGamps32.ini" "C:\Users\%name%" /c /g /h /i /k /q /s /y >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo xcopy "C:\Users\%name%.bak\AppData\Roaming\Microsoft\Windows\Themes" "C:\Users\%name%\AppData\Roaming\Microsoft\Windows\Themes" /c /g /h /i /k /q /s /y /exclude:C:\rbexclude.txt >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo Restoring The User's Local Settings... >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo xcopy "C:\Users\%name%.bak\AppData\Local\Microsoft\Windows\Themes" "C:\Users\%name%\AppData\Local\Microsoft\Windows\Themes" /c /g /h /i /k /q /s /y /exclude:C:\rbexclude.txt >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo Restoring Printers
echo regedit /ADD \\%comp%\c$\Temp\Printers.reg
echo echo.
echo cd /d "C:\Users\%name%" >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo cls >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo. >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo. >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo The profile data has been restored. You still need to: >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo 1) Re-map printers. >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo 2) Setup Outlook. >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo 3) Login MS Communicator. >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo 4) Manually delete C:\ProgramData\Microsoft\Windows\Start Menu >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat""
echo echo Start Menu\Programs\Startup\rebuild.bat (if present). >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo 5) Delete C:\Users\%name%.bak >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo when profile has been verified! >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo echo. >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo pause >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo attrib -h -r -s C:\rbexclude.txt >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo del /q C:\rbexclude.txt >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo attrib -h -r -s "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat" >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo del /s /q "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat" >>"%drv%\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\rebuild.bat"
echo .tmp >"%drv%\rbexclude.txt"
echo .err >>"%drv%\rbexclude.txt"
echo .ost >>"%drv%\rbexclude.txt"
echo .oab >>"%drv%\rbexclude.txt"
echo .mp3 >>"%drv%\rbexclude.txt"
echo .mp4 >>"%drv%\rbexclude.txt"
echo .wma >>"%drv%\rbexclude.txt"
echo extend.dat >>"%drv%\rbexclude.txt"
echo extend.dat _ActiveCACBAK >>"%drv%\rbexclude.txt"
goto regedit
:failed
cls
color c0
echo.
echo ********************** ERROR ***********************
echo.
echo The %name% profile could not be backed up.
echo Unable to rebuild this profile.
echo This script will now exit.
echo.
echo ****************************************************
echo.
echo.
pause
exit
:regedit
cls
:regedit /EXPORT \\%comp%\c$\Temp\Printers.reg "HKEY_CURRENT_USER\Printers"
COLOR 4F
echo.
echo *************** WARNING WARNING WARNING WARNING WARNING ***********************
echo.
echo Before continuing, You must now delete the profile registry and related
echo GUID keys. Run REGEDIT as administrator!
echo.
echo HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
echo.
echo and
echo.
echo HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileGuid
echo.
echo NOTE: There is not GUID for 64 Bit Windows.
echo.
echo *************** WARNING WARNING WARNING WARNING WARNING ***********************
echo.
pause
COLOR 07
goto complete
:complete
cls
echo.
echo.
echo Profile rebuild is now ready to run!
echo.
echo Please have the user login now. You will still need to:
echo 1) Manually re-map printers.
echo 2) Manually setup Outlook.
echo 3) Manually login MS Communicator if not signed in already.
echo 4) Manually delete C:\ProgramData\Microsoft\Windows\
echo Start Menu\Programs\Startup\rebuild.bat (if present).
echo 7) Manually delete C:\Users\%name%.bak
echo when profile has been verified.
echo.
pause
:end
I actually just went though the same thing!! Ok so the following "code" I threw together is in batch because it was easy and lightweight. It ain't pretty but it gets the job done.
Steps it does:
Connects to the machine and user profile. If the machine is offline, it will tell you. If the username provided doesn't match a profile, it will return a list of profiles on the computer so you can match the caller to the correct profile.
Backs up EFS Certificates to desktop.
Network drives and printers backed up to a text file on user's desktop.
User is automatically logged off
Appends 'old_' to the beginning of the local profile name and the date and time of rebuild to the end.
Backup of the profile registry key exported the old profile's 'Contacts' folder and then deletes it from the hive.
Copies a 'Profile Migration Tool' to the user's Desktop. This will move all the data from the old profile to the rebuilt one. It also copies the "Map all PST files.vbs" file found in Fix Pack. The "Map all PST files.vbs" file is automatically launched after all the data is migrated
After all steps are complete, Outlook launches
Step 1: You need to backup their data so here's that. Save it as Backup.bat
#title Profile Data Backup Tool v1.53
#echo off
echo.
echo.
if exist %USERPROFILE%\desktop\EFSCertBackup.cer del %USERPROFILE%\desktop\EFSCertBackup.cer
>goto :deletePFXFile
:deletePFXFile
if exist %USERPROFILE%\desktop\EFSCertBackup.pfx del %USERPROFILE%\desktop\EFSCertBackup.pfx
goto :backupefscerts
echo.
:backupefscerts
REM back up EFS Certs
echo This is a password you will create. If you do not want to create a password, press 'Enter' twice.
echo.
cipher /r:%USERPROFILE%\desktop\EFSCertBackup
REM back up network drives
net use > %USERPROFILE%\Desktop\NetworkDrivesandPrinters.txt
REM back up network printers
Cscript %WINDIR%\System32\Printing_Admin_Scripts\en-US\Prnmngr.vbs -l >> %USERPROFILE%\Desktop\NetworkDrivesandPrinters.txt
REM backup the registry key
set THEME_REGKEY="HKLM\SOFTWARE\Wow6432Node\Network Associates\ePolicy Orchestrator\Agent"
set THEME_REGVAL=LoggedOnUser
REM Check for presence of key first.
reg query %THEME_REGKEY% /v %THEME_REGVAL% || (echo No theme name present! & pause & exit /b 1)
REM query the value. pipe it through findstr in order to find the matching line that has the value. only grab token 3 and the remainder of the line. %%b is what we are interested in here.
set THEME_NAME=
for /f "tokens=2,*" %%a in ('reg query %THEME_REGKEY% /v %THEME_REGVAL% ^| findstr %THEME_REGVAL%') do (
set THEME_NAME=%%b
)
REM replace any spaces with +
set THEME_NAME=%THEME_NAME: =+%
echo.
echo Please wait. This window will close automatically once the tool is done running.
echo Please inform the agent once this window closes.
wmic useraccount where (name='%THEME_NAME%') get sid >SID.txt
powershell -Command "(gc SID.txt) -replace 'SID', '' | Out-File SID.txt"
powershell -Command "(gc SID.txt) -replace ' ', '' | Out-File SID.txt"
powershell -Command "(gc SID.txt) | ? {$_.trim() -ne '' } | set-content SID.txt"
for /f "delims=" %%x in (SID.txt) do set SID=%%x
reg export "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%SID%" "%Userprofile%\contacts\oldprofileBackupKey.reg"
shutdown.exe /l /f
(goto) 2>nul & del "%~f0"
2. This will map any and all PST files the user has. Save it as "Map All PST Files.vbs"
On error resume next
Dim objFSO
Dim objWSH
Dim objWMIService
Dim colProcess
Dim objProcess
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWSH = CreateObject("Wscript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Dim strUserProfile
Dim OutlookOpen
strUserProfile = objWSH.ExpandEnvironmentStrings("%USERPROFILE%")
objWSH.Run "%COMSPEC% /c DIR " & CHR(34) & strUserProfile & "\*.pst" & CHR(34) & " /b /s>" & CHR(34) & strUserProfile & "\My Documents\PST Locations.txt""", 0, True
objWSH.Run "%COMSPEC% /c DIR H:\*.pst /b /s>>" & CHR(34) & strUserProfile & "\My Documents\PST Locations.txt""", 0, True
If Not objFSO.FileExists(strUserProfile & "\My Documents\PST Locations.txt") Then
Done()
End If
Dim AppOutlook, OutlookNS, Path, objfile, Line
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process")
For Each objProcess In colProcess
If objProcess.Name = "OUTLOOK.EXE" Then OutlookOpen = True
Next
Set AppOutlook = CreateObject("Outlook.Application")
If err.number <> 0 Then
Set AppOutlook = GetObject(,"Outlook.Application")
End If
Set OutlookNS = AppOutlook.GetNameSpace("MAPI")
Set objfile = objFSO.opentextfile(strUserProfile & "\My Documents\PST Locations.txt")
Do Until objfile.AtEndOfStream
Line=objfile.readline
If instr(Line, "No PST mappings found") Then
Done()
Else
OutlookNS.AddStore Line
End If
Loop
If Not OutlookOpen Then
AppOutlook.Session.Logoff
AppOutlook.Quit
End If
objfile.close
Set objfile = Nothing
Set AppOutlook = Nothing
Set OutlookNS = Nothing
Done()
Sub Done()
MSGBOX "Remapping of PST(s) is complete. Please ensure that your PST(s) have been remapped succesfully."
If Not OutlookOpen Then objWSH.Run "%COMSPEC% /c Start Outlook",0,False
objFSO.DeleteFile(strUserProfile & "\Desktop\Map All PST Files.vbs")
Wscript.Quit
End Sub
3. This is the part that will move all of their data from the old profile to the new one. Save it as "Profile Migration Tool.bat"
#echo off
set THEME_REGKEY="HKLM\SOFTWARE\Wow6432Node\Network Associates\ePolicy Orchestrator\Agent"
set THEME_REGVAL=LoggedOnUser
cls
REM Check for presence of key first.
reg query %THEME_REGKEY% /v %THEME_REGVAL% || (echo No theme name present! & pause & exit /b 1)
cls
REM query the value. pipe it through findstr in order to find the matching line that has the value. only grab token 3 and the remainder of the line. %%b is what we are interested in here.
set THEME_NAME=
for /f "tokens=2,*" %%a in ('reg query %THEME_REGKEY% /v %THEME_REGVAL% ^| findstr %THEME_REGVAL%') do (
set THEME_NAME=%%b
)
cls
set /p content=<oldprofile.txt
robocopy "%content%\Contacts" "C:\Users\%THEME_NAME%\Contacts" /MIR /E /E /COPY:DAT /IS /XA:SHT /XF *.ini /R:2 /W:5
robocopy "%content%\Downloads" "C:\Users\%THEME_NAME%\Downloads" /MIR /E /COPY:DAT /XA:SHT /XF *.ini /R:2 /W:5
robocopy "%content%\Documents" "C:\Users\%THEME_NAME%\Documents" /MIR /E /COPY:DAT /IS /XA:SHT /XD "My Music" /XD "My Pictures" /XD "My Videos" /XF *.ini /R:2 /W:5
robocopy "%content%\Desktop" "C:\Users\%THEME_NAME%\Desktop" /MIR /E /COPY:DAT /IS /XA:SHT /XF *.ini /R:2 /W:5
robocopy "%content%\Favorites" "C:\Users\%THEME_NAME%\Favorites" /MIR /E /COPY:DAT /IS /XA:SHT /XF *.ini /R:2 /W:5
robocopy "%content%\Links" "C:\Users\%THEME_NAME%\Links" /MIR /E /COPY:DAT /IS /XA:SHT /XF *.ini /R:2 /W:5
robocopy "%content%\Music" "C:\Users\%THEME_NAME%\Music" /MIR /E /COPY:DAT /IS /XA:SHT /XF *.ini /R:2 /W:5
robocopy "%content%\Pictures" "C:\Users\%THEME_NAME%\Pictures" /MIR /E /COPY:DAT /IS /XA:SHT /XF *.ini /R:2 /W:5
robocopy "%content%\Videos" "C:\Users\%THEME_NAME%\Videos" /MIR /E /COPY:DAT /IS /XA:SHT /XF *.ini /R:2 /W:5
robocopy "%content%" "C:\Users\%THEME_NAME%\Documents" /COPY:DAT /IS /XD /XF ntuser.* /XF *.ini /R:2 /W:5
killtask /im outlook.exe
cd %currentuser%\Desktop
wscript "MAP ALL PST Files.vbs" //e:vbscript
del oldprofile.txt
del "%~f0"&exit
4. This is the holy grail. The part that actually rebuilds the profile. It does have to be run as an admin. Save it as RAP.bat:
#title Rebuild a Profile Tool Version 3.0
#echo off
:beginning
set THEME_REGKEY="HKLM\SOFTWARE\Wow6432Node\Network Associates\ePolicy Orchestrator\Agent"
set THEME_REGVAL=LoggedOnUser
REM Check for presence of key first.
reg query %THEME_REGKEY% /v %THEME_REGVAL% || (echo No theme name present! & pause>nul & exit /b 1)
REM query the value. pipe it through findstr in order to find the matching line that has the value. only grab token 3 and the remainder of the line. %%b is what we are interested in here.
set THEME_NAME=
for /f "tokens=2,*" %%a in ('reg query %THEME_REGKEY% /v %THEME_REGVAL% ^| findstr %THEME_REGVAL%') do (
set THEME_NAME=%%b
)
set RAPPath="\\127.0.0.1\C$\Program Files\HDUTILS\RAP"
cls
:connectIn
set /p remotepc= Enter the remote computer name or IP address:
if exist \\%remotepc%\C$\Users\ goto :inputUsername
echo Instruct user to restart the machine and log in.
SET tmout=3
PING 1.2.1.2 -n 1 -w %tmout%000 > NUL
echo.
goto :connectIn
:inputUsername
set /p remoteuser= Enter remote user's username:
if exist \\%remotepc%\C$\Users\%remoteuser%\ goto :choice
echo Invalid username. Please verify the spelling and try again.
SET tmout=3
PING 1.2.1.2 -n 1 -w %tmout%000 > NUL
dir \\%remotepc%\C$\Users\
goto :inputUsername
echo.
:choice
echo.
FOR %%? IN (\\%remotepc%\C$\Users\%remoteuser%\NTUSER.DAT) DO (ECHO %remoteUser%'s %%~n?%%~x? file size is %%~z? bytes)
echo Profile corruption typically occurs around 5000000 bytes.
echo.
REM This user's NTUSER.DAT file is over by ___ bytes.
set /P continue=Do you want to continue rebuilding this profile?[Y/N]
if /I "%continue%" EQU "Y" goto :sendBackup
if /I "%continue%" EQU "y" goto :sendBackup
if /I "%continue%" EQU "N" goto :abandon
if /I "%continue%" EQU "n" goto :abandon
echo.
echo Error: Invalid Parameter. Please press either "y" or "n" to continue.
echo.
goto :choice
:abandon
cls
echo Standby. Restarting program...
SET tmout=3
PING 1.2.1.2 -n 1 -w %tmout%000 > NUL
goto :beginning
REM Copy over the EFS Cert backup file
:sendBackup
set remoteuserdesktop=\\%remotepc%\C$\Users\%remoteuser%\Desktop
robocopy %RAPPath%\ %remoteuserdesktop%\ Backup.bat
echo.
echo.
echo.
echo Instruct the user to run the file named Backup.bat on their desktop and
echo let you know when it finishes. When it is done running, the command window
echo will close and 3 files should have appeared on the Desktop. Tell them to
echo inform you when they see the Ctrl + Alt + Del screen.
echo.
echo.
echo.
:SIDexist
if exist \\%remotepc%\c$\users\%remoteuser%\Desktop\SID.txt goto :copySID
goto :SIDexist
:copySID
robocopy %remoteuserdesktop%\ %RAPPath%\ SID.txt
del %remoteuserdesktop%\SID.txt
echo Press any key to rename the profile...
echo.
echo.
pause>nul
:rename
Set CURRDATE=%TEMP%\CURRDATE.TMP
Set CURRTIME=%TEMP%\CURRTIME.TMP
DATE /T > %CURRDATE%
TIME /T > %CURRTIME%
Set PARSEARG="eol=; tokens=1,2,3,4* delims=/, "
For /F %PARSEARG% %%i in (%CURRDATE%) Do SET YYYYMMDD=%%l%%k%%j
Set PARSEARG="eol=; tokens=1,2,3* delims=:, "
For /F %PARSEARG% %%i in (%CURRTIME%) Do Set HHMM=%%i%%j%%k
Echo RENAME \\%remotepc%\C$\Users\%remoteuser% old_%remoteuser%_%YYYYMMDD%%HHMM%
RENAME \\%remotepc%\C$\Users\%remoteuser% old_%remoteuser%_%YYYYMMDD%%HHMM%
set oldusername=old_%remoteuser%_%YYYYMMDD%%HHMM%
echo old_%remoteuser%_%YYYYMMDD%%HHMM% >oldprofile.txt
powershell -Command "(gc oldprofile.txt) -replace ' ', '' | Out-File oldprofile.txt"
powershell -Command "(gc oldprofile.txt) | ? {$_.trim() -ne '' } | set-content oldprofile.txt"
:choice2
if exist \\%remotepc%\C$\Users\old_%remoteuser%_%YYYYMMDD%%HHMM% goto :regKeyDelete
goto :uhoh
:uhoh
echo Inform the user you will restart their computer momentarily. Tell them to let
echo you know when they see the 'Ctrl + Alt + Del'screen.
echo Verify network connectivity with the ping that is now running.
shutdown /r /m \\%remotepc% /c "Computer will not rename profile. When prompted for a green shutdown, press no. Press OK to continue." /d p:0:0
start cmd.exe #cmd /k "ping -t %remotepc%"
echo.
echo Wait 60 seconds after the machine comes back online, then press any key to
echo continue the rebuild...
pause>nul
goto :rename2
:rename2
Set CURRDATE=%TEMP%\CURRDATE.TMP
Set CURRTIME=%TEMP%\CURRTIME.TMP
DATE /T > %CURRDATE%
TIME /T > %CURRTIME%
Set PARSEARG="eol=; tokens=1,2,3,4* delims=/, "
For /F %PARSEARG% %%i in (%CURRDATE%) Do SET YYYYMMDD=%%l%%k%%j
Set PARSEARG="eol=; tokens=1,2,3* delims=:, "
For /F %PARSEARG% %%i in (%CURRTIME%) Do Set HHMM=%%i%%j%%k
Echo RENAME \\%remotepc%\C$\Users\%remoteuser% old_%remoteuser%_%YYYYMMDD%%HHMM%
RENAME \\%remotepc%\C$\Users\%remoteuser% old_%remoteuser%_%YYYYMMDD%%HHMM%
set oldusername=old_%remoteuser%_%YYYYMMDD%%HHMM%
echo old_%remoteuser%_%YYYYMMDD%%HHMM% >oldprofile.txt
powershell -Command "(gc oldprofile.txt) -replace ' ', '' | Out-File oldprofile.txt"
powershell -Command "(gc oldprofile.txt) | ? {$_.trim() -ne '' } | set-content oldprofile.txt"
goto :choice3
:choice3
echo.
if exist \\%remotepc%\C$\Users\old_%remoteuser%_%YYYYMMDD%%HHMM% goto :regKeyDelete
goto :rename2
:regKeyDelete
REM Delete registry key
cd "C:\Program Files\HDUTILS\RAP\"
set /p Build=<SID.txt
reg delete "\\%remotepc%\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%Build%"
echo \\%remotepc%\C$\Users\old_%remoteuser%_%YYYYMMDD%%HHMM% >oldprofile.txt
echo.
echo.
echo Instruct the User to log in.
SET tmout=3
PING 1.2.1.2 -n 1 -w %tmout%000 > NUL
echo Waiting on customer login...
echo.
:listrebuilt
if exist \\%remotepc%\C$\Users\%remoteuser%\Desktop goto :nextplease
goto :listrebuilt
:nextplease
set remoteuserdesktop=\\%remotepc%\C$\Users\%remoteuser%
powershell -Command "(gc oldprofile.txt) -replace ' ', '' | Out-File oldprofile.txt"
powershell -Command "(gc oldprofile.txt) | ? {$_.trim() -ne '' } | set-content oldprofile.txt"
:copymigrationTools
robocopy %RAPPath%\ %remoteuserdesktop%\ oldprofile.txt
robocopy %RAPPath%\ %remoteuserdesktop%\ "Profile Migration Tool.bat"
robocopy %RAPPath%\ %remoteuserdesktop%\ "Map All PST files.vbs"
:checkFiles
if exist %remoteuserdesktop%\oldprofile.txt goto :checkPMT
goto :recopyProfile
:checkPMT
if exist "%remoteuserdesktop%\Profile Migration Tool.bat" goto :checkPST
goto :recopyPMT
:checkPST
if exist "%remoteuserdesktop%\Map All PST files.vbs" goto :finish
goto :recopyPST
:recopyProfile
robocopy %RAPPath%\ %remoteuserdesktop%\ oldprofile.txt
goto :checkFiles
:recopyPMT
robocopy %RAPPath%\ %remoteuserdesktop%\ "Profile Migration Tool.bat"
goto :checkFiles
:recopyPST
robocopy %RAPPath%\ %remoteuserdesktop%\ "Map All PST files.vbs"
goto :checkFiles
:finish
echo.
echo You have completed your end of the rebuild. MSRA in (to monitor everything)
echo and run 'Profile Migration Tool.bat' file to begin the migration process.
echo While everything is migrating, verify the user knows how to map network drives
echo and printers.
echo restart in 20 seconds.
(
echo Profile Rebuilt
echo Backup up EFS Certificates to desktop.
echo Network drives and printers backed up to a text file on user's desktop.
echo Appended 'old_' to the beginning of the local profile name and the date and time of rebuild to the end.
echo Backup of the profile registry key exported the old profile's 'Contacts' folder.
echo Agent instructed the user to log in.
echo Copied the Profile Migration Tool to the user's Desktop.
echo Data copied from old profile, EFS certificates restored ) | clip
echo.
echo A copy of all steps taken have been sent to the clipboard. Please paste these into your ticket.
SET tmout=10
PING 1.2.1.2 -n 1 -w %tmout%000 > NUL
goto :beginning

Resources