2

Having a hard time figuring this one out. I have a vbscript that asks for username and password. Then the script runs PSExec.exe passing those to the command line like this.

    strUser = Inputbox("Username:","Username")
    strPassword = Inputbox("Password:","Password")
    Set objShell = WScript.CreateObject("WScript.Shell")
    objShell.Run "%comspec% /K psexec.exe \\workstation -u " & struser _
         & " -p " & strPassword & " -d calc.exe", 1, false

This works fine if the password doesn't have a caret in it. BUT if it does have one, for example if the password is "Pass)(*&^%$#@!" I get the following error from psexec.

'%$#@!' is not recognized as an internal or external command,
operable program or batch file.

The caret is being read by the command line as a space so it thinks it's trying to run this command.

psexec.exe \\workstation64 -u User -p Pass)(*& %$#@! -d Calc.exe

As you can see there is a space instead of a caret so psexec see's %$#@! as the command.

I've seen an example for passing it when using a batch file but it doesn't work in this case. It said adding an extra ^ like this ^^ works in a bat file.

How can I pass a caret to the command line????

UPDATE......

I worked on this for about 45 minutes today at work and couldn't get it to work. First thing I tried was to add quotes to the password and it still didn't work. I just tested it here at the house and it worked fine....... OS at work is Windows XP and at home I run Windows 7. I will try again in the morning to make sure I didn't mistype something. Here is what I did to make it work at home on Windows 7.

    strUser = Inputbox("Username:","Username")
    strPassword = Inputbox("Password:","Password")

    strPassword = chr(34) & strPassword & chr(34)

    Set objShell = WScript.CreateObject("WScript.Shell")
    objShell.Run "%comspec% /K psexec.exe \\workstation -u " & struser _
         & " -p " & strPassword & " -d calc.exe", 1, false
  • 1
    From the SO answer link at end of this comment, 4 carets are required: http://stackoverflow.com/a/5254713/1569434 – Lizz Jan 30 '14 at 21:44

1 Answers1

0

The problem lays in using of ampersand, not caret: single ampersand is used as a command separator, so rest of the line after &-sign considered to be a next command by cmd interpreter.

In next example I have used SET command instead of PSExec, as I will not experiment with PSExec.

All goes well with SET command, even if escaped all characters:), but I'm not sure about percentage sign, which is said "must be doubled to use literally".

Dim strPssw: strPssw = "/Pass"")=(*&^%$#@!"
Dim ii, strChar, strEscape
Dim strPassword: strPassword = ""
For ii = 1 To Len( strPssw) 'cannot use Replace() function
  strChar = Mid( strPssw, ii, 1)
  Select Case strChar
  Case "&", """", "^", "%", "=", _
       "@", "(", ")", "!", "*", "?", _
       ".", "\", "|", ">", "<", "/"
    strEscape = "^"
  Case Else
    strEscape = "" 'all goes well even if strEscape = "^" here
  End Select
    strPassword = strPassword & strEscape & strChar
Next
Dim objShell: Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "%comspec% /C set varia=" & strPassword & "&set varia&pause&set varia=", 1, false

' @  - At Symbol: be less verbose
' &  - Single Ampersand: used as a command separator
' ^  - Caret: general escape character in batch
' "  - Double Quote: surrounding a string in double quotes escapes all of the characters contained within it
' () - Parentheses: used to make "code blocks" of grouped commands
' %  - Percentage Sign: are used to mark some variables, must be doubled to use literally
' !  - Exclamation Mark: to mark delayed expansion environment variables !variable!
' *  - Asterisk: wildcard matches any number or any characters
' ?  - Question Mark: matches any single character
' .  - Single dot: represents the current directory
' \  - Backslash: represent the root directory of a drive dir ^\
' |  - Single Pipe: redirects the std.output of one command into the std.input of another
' >  - Single Greater Than: redirects output to either a file or file like device
' <  - Less Than: redirect the contents of a file to the std.input of a command
''
' && - Double Ampersand: conditional command separator (if errorlevel 0)
' .. - Double dot: represents the parent directory of the current directory
' || - Double Pipe: conditional command separator (if errorlevel > 0)
' :: - Double Colon: alternative to "rem" for comments outside of code blocks
' >> - Double Greater than: output will be added to the very end of the file
' thanks to http://judago.webs.com/batchoperators.htm
'
JosefZ
  • 28,460
  • 5
  • 44
  • 83