Batch file help

I am very new at writing batch files, so please excuse my ignorance.  I have written a simple batch file that allows me to go into my active directory domain and unlock an account and change their password.  The problem is when you unlock their account it closes the program and you have to reopen it back up if you want to change their password.  How can I change the batch file to allow it to stay open so I can perform an unlock and a password change without the program closing in between.  Here is a copy of my batch file.
Thanks for the help.
Greg

@echo off

echo.

set /p loginid="Please enter the user ID:"

echo.

set /p reset="To unlock the account press "N"   To reset the password press "Y"

echo.

if (%reset%)==(n) goto noreset

set /p newpassword="Enter a new password:"

echo.

NET USER %loginid% %newpassword% /DOMAIN /ACTIVE:YES

echo User ID %loginid% has been unlocked and the password set to %newpassword%.

goto end

:noreset

NET USER %loginid% /DOMAIN /ACTIVE:YES

echo User ID %loginid% has been unlocked.

echo.

:end

pause
Greg StewartNetwork AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Maybe something like this?
The script is in test mode and will only display the "net.exe" commands it would run otherwise. Remove the uppercase ECHOs in lines 26 and 32 to run it for real.
@echo off
setlocal
:PickUser
echo.
set /p LoginId=Please enter the user ID:
net.exe user "%LoginId%" /domain >NUL
if errorlevel 1 goto PickUser
:ProcessUser
echo.
echo Selected user: '%LoginId%'
echo.
echo [U] Unlock the account
echo [P] Reset the Password and unlock
echo [C] Change the user
echo [X] Leave the script
echo.
set /p Choice=Please select: 
if /i "%Choice%"=="U" goto UnlockUser
if /i "%Choice%"=="P" goto PasswordReset
if /i "%Choice%"=="C" goto PickUser
if /i "%Choice%"=="X" goto :eof
goto :ProcessUser

:PasswordReset
set /p NewPassword=Enter the new password: 
ECHO net.exe user "%LoginId%" "%NewPassword%" /DOMAIN /ACTIVE:YES
echo.
echo User ID '%LoginId%' has been unlocked and the password set to '%NewPassword%'.
goto ProcessUser

:UnlockUser
ECHO net.exe user "%LoginId%" /DOMAIN /ACTIVE:YES
echo.
echo User ID %LoginId% has been unlocked.
goto ProcessUser

Open in new window

Edit: mentioned test mode.
0
Steve KnightIT ConsultancyCommented:
How about this for starters:

@echo off
echo.
set /p loginid="Please enter the user ID:"
echo.

:start
set /p reset=To unlock the account press "U"   To reset the password press "P"
echo.

if "%reset%"=="P" (
  set /p newpassword="Enter a new password:"
  NET USER %loginid% %newpassword% /DOMAIN /ACTIVE:YES
  echo User ID %loginid% has been unlocked and the password set to %newpassword%.
)

if "%reset%"=="U" (
  NET USER %loginid% /DOMAIN /ACTIVE:YES
  echo User ID %loginid% has been unlocked.
)

echo.
set reset=
set /p reset=To unlock the same account press "U"   To reset the password press "P"  Press Return to finish
if not "%reset%"=="" goto start

Open in new window


We can add to that with a GUI input:
0
oBdACommented:
And just for the fun of it, this will show you the user's full name as well, in case the user ID had a typo (as before in test mode):
@echo off
setlocal
:PickUser
echo.
set /p LoginId=Please enter the user ID:
for /f "tokens=2*" %%a in ('net.exe user "%LoginId%" /domain ^| find.exe /i "Full Name"') do set FullName=%%b
if errorlevel 1 goto PickUser
:ProcessUser
echo.
echo Selected user: '%LoginId%' ^(%FullName%^)
echo.
echo [U] Unlock the account
echo [P] Reset the Password and unlock
echo [C] Change the user
echo [X] Leave the script
echo.
set /p Choice=Please select: 
if /i "%Choice%"=="U" goto UnlockUser
if /i "%Choice%"=="P" goto PasswordReset
if /i "%Choice%"=="C" goto PickUser
if /i "%Choice%"=="X" goto :eof
goto :ProcessUser

:PasswordReset
set /p NewPassword=Enter the new password: 
ECHO net.exe user "%LoginId%" "%NewPassword%" /DOMAIN /ACTIVE:YES
echo.
echo User ID '%LoginId%' has been unlocked and the password set to '%NewPassword%'.
goto ProcessUser

:UnlockUser
ECHO net.exe user "%LoginId%" /DOMAIN /ACTIVE:YES
echo.
echo User ID %LoginId% has been unlocked.
goto ProcessUser

Open in new window

0
Steve KnightIT ConsultancyCommented:
GUI version in batch, save as .CMD or .BAT file as normal.

Steve

@echo off

call :InputBox "Please enter the user ID:"
if "%input%"=="" exit /b
set loginid=%input%

:start
Set YesNo=
call :YesNoBox "Unlock account?"
Set Unlock=%YesNo%

call :YesNoBox "Reset Password?"
Set Reset=%YesNo%

if "%reset%"=="6" (
  call InputBox "Enter a new password:"
  set newPassword=%Input%
  NET USER %loginid% %newpassword% /DOMAIN /ACTIVE:YES
  call :MessageBox  "User ID %loginid% has been unlocked and the password set to %newpassword%."
)

if "%Unlock%"=="6" (
  NET USER %loginid% /DOMAIN /ACTIVE:YES
  call :MessageBox "User ID %loginid% has been unlocked."
)

exit /b

:InputBox
REM http://scripts.dragon-it.co.uk/links/batch-gui-inputbox
set input=
set heading=%~2
set message=%~1
echo wscript.echo inputbox(WScript.Arguments(0),WScript.Arguments(1)) >"%temp%\input.vbs"
for /f "tokens=* delims=" %%a in ('cscript //nologo "%temp%\input.vbs" "%message%" "%heading%"') do set input=%%a
exit /b

:YesNoBox
REM http://scripts.dragon-it.co.uk/links/batch-gui-yesno-messagebox
REM returns 6 = Yes, 7 = No. Type=4 = Yes/No
set YesNo=
set MsgType=4
set heading=%~2
set message=%~1
echo wscript.echo msgbox(WScript.Arguments(0),%MsgType%,WScript.Arguments(1)) >"%temp%\input.vbs"
for /f "tokens=* delims=" %%a in ('cscript //nologo "%temp%\input.vbs" "%message%" "%heading%"') do set YesNo=%%a
exit /b

:MessageBox
REM http://scripts.dragon-it.co.uk/links/batch-gui-yesno-messagebox
set heading=%~2
set message=%~1
echo msgbox WScript.Arguments(0),0,WScript.Arguments(1) >"%temp%\input.vbs"
cscript //nologo "%temp%\input.vbs" "%message%" "%heading%"
exit /b

Open in new window

0
oBdACommented:
Sorry, disregard http:#a40820773; the user checking is incorrect. Fixed version:
@echo off
setlocal
:PickUser
echo.
set /p LoginId=Please enter the user ID:
set UserFound=False
for /f "tokens=2*" %%a in ('net.exe user "%LoginId%" /domain ^| find.exe /i "Full Name"') do (
	set UserFound=True
	set FullName=%%b
)
if /i "%UserFound%"=="False" goto PickUser
:ProcessUser
echo.
echo Selected user: '%LoginId%' ^(%FullName%^)
echo.
echo [U] Unlock the account
echo [P] Reset the Password and unlock
echo [C] Change the user
echo [X] Leave the script
echo.
set /p Choice=Please select: 
if /i "%Choice%"=="U" goto UnlockUser
if /i "%Choice%"=="P" goto PasswordReset
if /i "%Choice%"=="C" goto PickUser
if /i "%Choice%"=="X" goto :eof
goto :ProcessUser

:PasswordReset
set /p NewPassword=Enter the new password: 
ECHO net.exe user "%LoginId%" "%NewPassword%" /DOMAIN /ACTIVE:YES
echo.
echo User ID '%LoginId%' has been unlocked and the password set to '%NewPassword%'.
goto ProcessUser

:UnlockUser
ECHO net.exe user "%LoginId%" /DOMAIN /ACTIVE:YES
echo.
echo User ID %LoginId% has been unlocked.
goto ProcessUser

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.