使用VBScript将插入符号传递到命令行

使用VBScript将插入符号传递到命令行,vbscript,cmd,caret,Vbscript,Cmd,Caret,很难弄明白这一点。我有一个vbscript,要求输入用户名和密码。然后,脚本运行PSExec.exe,将这些文件传递到命令行,如下所示 strUser = Inputbox("Username:","Username") strPassword = Inputbox("Password:","Password") Set objShell = WScript.CreateObject("WScript.Shell") objShell.Run "%comspec%

很难弄明白这一点。我有一个vbscript,要求输入用户名和密码。然后,脚本运行PSExec.exe,将这些文件传递到命令行,如下所示

    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
如果密码中没有插入符号,则此操作可以正常工作。但是如果它确实有一个,例如,如果密码是Pass*&^%$@!我从psexec得到以下错误

'%$#@!' is not recognized as an internal or external command,
operable program or batch file.
插入符号被命令行作为空格读取,因此它认为它正在尝试运行此命令

psexec.exe \\workstation64 -u User -p Pass)(*& %$#@! -d Calc.exe
正如您所看到的,有一个空格而不是插入符号,因此psexec see的%$@!作为命令

我见过一个在使用批处理文件时传递它的示例,但在这种情况下它不起作用。它说在bat文件中添加一个额外的^

如何将插入符号传递到命令行

更新

今天上班的时候,我花了大约45分钟来做这件事,但都没能成功。我尝试的第一件事是在密码中添加引号,但仍然不起作用。我刚在家里测试过,效果很好。。。。。。。工作时的操作系统是WindowsXP,在家里我运行Windows7。我会在早上再试一次,以确保我没有键入错误的东西。以下是我在Windows7上为使其在家中工作所做的工作

    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

问题在于使用了符号AND,而不是插入符号:单个符号AND用作命令分隔符,因此&-符号后的其余行被cmd解释器视为下一个命令

在下一个示例中,我使用了SET命令而不是PSExec,因为我不会尝试PSExec

SET命令一切顺利,即使转义了所有字符:,但我不确定百分号,据说必须加倍才能使用

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
'

在本评论末尾的SO答案链接中,需要4个插入符号: