Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

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

0
nappy_d
Asked:
nappy_d
  • 3
2 Solutions
 
Gabriel CliftonCommented:
First thing I see: line 5 and line 9, uname, try calling line 5 something else like unamed.
0
 
Gabriel CliftonCommented:
Line 20 you are searching for a blank password but yet you have IF "%passwd%"=="y", change to IF "%passwd%"==""
0
 
Gabriel CliftonCommented:
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
 
nappy_dAuthor 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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now