Solved

batch scripting

Posted on 2014-07-18
5
280 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 83

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
factory design pattern vs abstract factoy design pattern 2 84
Hibernate on windows 10 18 80
countX 22 70
count11 challenge 6 47
Have you ever had a hard drive that you can't boot into, but need to change the registry? Here is the solution! This article guides you through accessing and editing a registry of a non-primary drive. To read registry information on a non-prim…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now