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

x
?
Solved

set batch file variables containing special characters

Posted on 2009-04-16
8
Medium Priority
?
2,838 Views
Last Modified: 2013-11-10
Hello,

I'm working on a set of batch files that automate the "post-workstation imaging" tasks.  Currently, I have a series of 3 batch files --  They are 99% functional -- the issue I'm running into is with users who have special characters in their passwords  (like the & symbol).  I'll use the following lines to prompt the user for their admin credentials:

set /p adun="Type your admin username: DA\"
%gctmp%\conset /ph adpw=Type your admin password:

I then use the %adun% and %adup% variables to query active directory using dsquery/dsget, and later to add machines to the domain using netdom and move them to the correct ou using dsmove.  Of course when I do this, and the user has a special character in their password (like the & ), it cuts off everything from the "&" back.  

I've found if I set a variable using quotes, it works fine, but the variable is being set from the user's input, and short of asking them to "surround your password with quotes if it contains special characters", is there another way to do this?  

Of course, if the user's password doesn't contain any special characters, the scripts run fine, but our passwords must contain at least 2 special characters...

Also, a side-note:  I'm using the conset program to set the adpw variable so the user's passowrd is not echoed back to them (or anyone else looking over their shoulder), but it appears to work the same way as the set command.  

Thanks.
 
0
Comment
Question by:damoncf1234
  • 4
  • 3
8 Comments
 
LVL 72

Expert Comment

by:Qlemo
ID: 24159551
In most cases it is sufficient to use the variable in quotes, as in

echo. "%adpw%"
0
 

Author Comment

by:damoncf1234
ID: 24159801
Thanks for the reply...

The issue appears to be "setting" the variable that contains special characters (like the & for example).  If I go to a command prompt and type in:

set adpw=abc&123

It doesn't set adpw to equal abc&123, it responds with the following error message:
'123' is not recognized as an internal or external command, operable program or batch file.  

If I enclose the variable in quotes, it works fine...  The problem is, I'm using the /P switch and setting the variable using input that the user types in...  And I can't find a way to surround what they type at the prompt in quotes as the variable is being set.  Of course I could probably ask them to "surround their password with quotes", but would prefer not to do that (if possible).  
0
 
LVL 72

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 24159888
The user can type ampersand when using set /p. However, set adpw=abc&123  will not work.
You will not have to quote the input in set /p, but the usage in your batch file. Switching to delayed expansion could also help, as the variable contents will be substituted AFTER the command is parsed (does only work in a batch file):

setlocal EnableDelayedEpansion
set /P adpw=Type pwd: & echo !adpw!

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:damoncf1234
ID: 24160232
OK, thanks.

So basically, I can just enclose the %adpw% in quotes when used throughout the batch file?

If I'm using the adpw variable as part of a switch for another command (like the -p switch for dsquery & dsget, as well as netdom & dsmove), the quotes won't appear to dsquery and the other programs as  part of the password itself?

 
dsquery computer "dc=xxx,dc=mil" -d xxx.mil -u %adun% -p "%adpw%" -name %nameprefix%* -limit 150 | dsget computer -d xxx.mil -u %adun% -p "%adpw%" -samid
 
netdom join %computername% /Domain:xxx /UserD:dia\%adun% /PasswordD:"%adpw%"
 
dsmove  "cn=%computername%,cn=Computers,dc=xxx,dc=mil" -newparent "ou=XPClients,ou=%locOU%,ou=Workstations,ou=Corporate,ou=Resources,dc=xxx,dc=mil" -d xxx.mil -u xxx\%adun% -p "%adpw%"

Open in new window

0
 

Author Comment

by:damoncf1234
ID: 24160422
Qlemo,

Thanks.  I just tested the dsquery/dsget commands using the quotes like you suggested, and they appear to interpret passwords with special characters now...  I'm ghosting a machine now so I can test the netdom & dsmove commands with the quotations as well.  Hopefully it works.  
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24161865
Just wiait till an admin decides to use quotes in their passwords :-)
0
 
LVL 72

Expert Comment

by:Qlemo
ID: 24163366
(At least) the following characters are not usable, and would require further processing: "!%
Exclamation mark can be used as long as you omit the setlocal EnableDelayedExpansion clause and have delayed expansion not enabled by default (via Registry).


0
 

Author Comment

by:damoncf1234
ID: 24163873
Qlemo,
Thanks, the quotes did the trick.  I just tested it all the way through and it worked like a charm.  

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Simple Linear Regression

571 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