Link to home
Start Free TrialLog in
Avatar of damoncf1234
damoncf1234

asked on

set batch file variables containing special characters

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.
 
Avatar of Qlemo
Qlemo
Flag of Germany image

In most cases it is sufficient to use the variable in quotes, as in

echo. "%adpw%"
Avatar of damoncf1234
damoncf1234

ASKER

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).  
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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.  
Just wiait till an admin decides to use quotes in their passwords :-)
(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).


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