Solved

batch scripting

Posted on 2014-07-18
5
305 Views
Last Modified: 2014-07-19
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
Comment
Question by:nappy_d
  • 3
5 Comments
 
LVL 13

Expert Comment

by:Gabriel Clifton
ID: 40204682
First thing I see: line 5 and line 9, uname, try calling line 5 something else like unamed.
0
 
LVL 13

Expert Comment

by:Gabriel Clifton
ID: 40204692
Line 20 you are searching for a blank password but yet you have IF "%passwd%"=="y", change to IF "%passwd%"==""
0
 
LVL 13

Assisted Solution

by:Gabriel Clifton
Gabriel Clifton earned 250 total points
ID: 40204714
And line 60 & 62 :disconnectQ
0
 
LVL 84

Accepted Solution

by:
oBdA earned 250 total points
ID: 40204784
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
 
LVL 32

Author Closing Comment

by:nappy_d
ID: 40206339
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
post4 challenge 28 104
object oriented javascript web form 8 145
convert Systemjs to Webpack 3 34
troubleshoot a python script 8 24
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question