batch scripting

Hi everyone,

I have this batch script I pieced together. Been looking at this for too long and need a second pair of eyes The problems are with lines 9 and 20.  They are not going to the errors when prompted.

Thanks for the input.


@ECHO OFF
REM Clear Screen of information
cls
REM Prompt for username:uname
:uname

REM If username is blank go to Error prompt.  If username is entered prompt for password
SET /P uname=Please enter your username: 
IF "%uname%"=="" (goto uNameError) else (goto password)

REM prompt for password
:password

REM this will mask password that is being entered
@echo off
set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
    $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
        [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set passwd=%%p
IF "%passwd%"=="y" (goto passwdError) else (goto rasdial)

REM If password is blank go to password Error prompt.  If password is entered start dialing VPN connection
REM SET /P passwd=Please enter your password:
REM IF "%passwd%"=="y" (goto passwdError) else (goto rasdial)


REM Dialing VPN COnnection
:rasdial

Echo Trying to connect to VPN................

rasdial VPN %uname% %passwd%

REM If VPN connection is not successful, error message will be displayed
 if NOT %ERRORLEVEL% ==0 (goto failed) else (goto mapDrive)

REM Prompt to retry VPN connection ir exit
:failed
SET /P rasFailed=Failed to connect......Retry? (y or press n then enter to exit):
IF "%rasFailed%"=="y" (goto uname) else IF "%rasFailed%"=="n" (goto end)

REM Network Drive connections
:mapDrive
net use w: \\Server\database /user:%uname% %passwd% /Persistent:no
goto disconnectQ

REM Username is Blank Error MEssage
:uNameError
ECHO You did not enter a username.
SET /P unameretry=Retry? (y, then enter or press enter to exit):
IF "%unameretry%"=="y" (goto uname) else (goto end)

REM Password is Blank Error Message
:passwdError
ECHO You did not enter a password.
SET /P passRetry=Retry? (y, then enter or press enter to exit):
IF "%passRetry%"=="y" (goto password) else (goto end)

REM Propmt for disconnection from VPN
:disconnectQ
@echo off
SET /P disconnectQ=Are you ready to disconnect?
IF "%disconnectQ%"=="y" (goto logoffVPN) else goto disconnectQ

REM Disconnect Mapped VPN Drive and then disconnect from VPN
:logoffVPN
net use w: /delete
rasdial VPNName  /DISCONNECT

:end

Open in new window

LVL 32
nappy_dThere are a 1000 ways to skin the technology cat.Asked:
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.

Gabriel CliftonNet AdminCommented:
First thing I see: line 5 and line 9, uname, try calling line 5 something else like unamed.
0
Gabriel CliftonNet AdminCommented:
Line 20 you are searching for a blank password but yet you have IF "%passwd%"=="y", change to IF "%passwd%"==""
0
Gabriel CliftonNet AdminCommented:
And line 60 & 62 :disconnectQ
0
oBdACommented:
The issue is not the identical label and variable names.
1. If nothing is entered in a "set /p" query, the variable will keep its previous value.
2. "for /f" will not execute the "do" command if there is no input, which means (as with "set /p") that if nothing was entered in the powershell command, passwd will still have its previous value as well.
3. You didn't use "setlocal" in your script, and probably are testing from an open command prompt. Without "setlocal", all variables set in the script will remain available in the prompt and keep their values in the next run, so once uname and passwd were set, they'll never make it back to empty.
Finally, if you make string comparisons, use "if /i ..." to make a case insensitive comparison, so that "Y" will be accepted as well as "y".
Easy fix: reset the variables before the queries (and correct the obvious comparison error in line 20 that PantherTech already pointed out):
@ECHO OFF
setlocal
REM Clear Screen of information
cls
REM Prompt for username:uname
:uname

REM If username is blank go to Error prompt.  If username is entered prompt for password
SET uname=
SET /P uname=Please enter your username: 
IF "%uname%"=="" (goto uNameError) else (goto password)

REM prompt for password
:password

REM this will mask password that is being entered
set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
    $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
        [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
set passwd=
for /f "usebackq delims=" %%p in (`%psCommand%`) do set passwd=%%p
IF "%passwd%"=="" (goto passwdError) else (goto rasdial)

REM If password is blank go to password Error prompt.  If password is entered start dialing VPN connection
REM SET /P passwd=Please enter your password:
REM IF "%passwd%"=="y" (goto passwdError) else (goto rasdial)


REM Dialing VPN COnnection
:rasdial

Echo Trying to connect to VPN................

rasdial VPN %uname% %passwd%

REM If VPN connection is not successful, error message will be displayed
 if NOT %ERRORLEVEL% ==0 (goto failed) else (goto mapDrive)

REM Prompt to retry VPN connection ir exit
:failed
SET rasFailed=
SET /P rasFailed=Failed to connect......Retry? (y or press n then enter to exit):
IF /i "%rasFailed%"=="y" (goto uname) else IF "%rasFailed%"=="n" (goto end)

REM Network Drive connections
:mapDrive
net use w: \\Server\database /user:%uname% %passwd% /Persistent:no
goto disconnectQ

REM Username is Blank Error MEssage
:uNameError
ECHO You did not enter a username.
SET unameretry=
SET /P unameretry=Retry? (y, then enter or press enter to exit):
IF /i "%unameretry%"=="y" (goto uname) else (goto end)

REM Password is Blank Error Message
:passwdError
ECHO You did not enter a password.
SET passRetry=
SET /P passRetry=Retry? (y, then enter or press enter to exit):
IF /i "%passRetry%"=="y" (goto password) else (goto end)

REM Propmt for disconnection from VPN
:disconnectQ
@echo off
SET disconnectQ=
SET /P disconnectQ=Are you ready to disconnect?
IF /i "%disconnectQ%"=="y" (goto logoffVPN) else goto disconnectQ

REM Disconnect Mapped VPN Drive and then disconnect from VPN
:logoffVPN
net use w: /delete
rasdial VPNName  /DISCONNECT

:end

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
nappy_dThere are a 1000 ways to skin the technology cat.Author Commented:
Thanks everyone.  Here is my final script.  I made a few changes and corrected the errors you all assisted me with and it works great.

 @ECHO OFF
setlocal
REM Clear Screen of information
cls
REM Prompt for username:uname
:uname

REM If username is blank go to Error prompt.  If username is entered prompt for password
SET uname=
SET /P uname=Please enter your username: 
IF "%uname%"=="" (goto uNameError) else (goto password)

REM prompt for password
:password

REM this will mask password that is being entered
set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
    $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
        [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
set passwd=
for /f "usebackq delims=" %%p in (`%psCommand%`) do set passwd=%%p
IF "%passwd%"=="" (goto passwdError) else (goto rasdial)

REM If password is blank go to password Error prompt.  If password is entered start dialing VPN connection
REM SET /P passwd=Please enter your password:
REM IF "%passwd%"=="y" (goto passwdError) else (goto rasdial)


REM Dialing VPN COnnection
:rasdial

Echo Trying to connect to VPN................

rasdial target %uname% %passwd%
 if NOT %ERRORLEVEL% ==0 (goto failed) else (goto runApps)

REM Prompt to retry VPN connection
:failed
REM SET rasFailed=
SET /P rasFailed=Failed to connect......Retry? (y or press n then enter to exit):
IF /i "%rasFailed%"=="y" (goto uname) else IF "%rasFailed%"=="n" (goto runApps)

REM Run post VPN Logon Actions
:runApps
Start RDP connection mstsc /v:ServerName /f
goto disconnectQ

REM Username is Blank Error MEssage
:uNameError
ECHO You did not enter a username.
SET unameretry=
SET /P unameretry=Retry? (y, then enter or press enter to exit):
IF /i "%unameretry%"=="y" (goto uname) else (goto end)

REM Password is Blank Error Message
:passwdError
ECHO You did not enter a password.
SET passRetry=
SET /P passRetry=Retry? (y, then enter or press enter to exit):
IF /i "%passRetry%"=="y" (goto password) else (goto end)

REM Propmt for disconnection from VPN
:disconnectQ
@echo off
SET disconnectQ=
SET /P disconnectQ=Are you ready to disconnect? (Type y and press enter)
IF /i "%disconnectQ%"=="y" (goto logoffVPN) else IF /i "%disconnectQ%"=="n"(goto disconnectQ)

REM disconnect from VPN
:logoffVPN
rasdial VPN  /DISCONNECT

:end

Open in new window

0
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
Microsoft Legacy OS

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.