2

很难弄清楚这一点。我有一个询问用户名和密码的 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 看到 %$#@! 作为命令。

我已经看到一个使用批处理文件时传递它的示例,但在这种情况下它不起作用。它说像这样添加一个额外的 ^ ^^ 可以在 bat 文件中使用。

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

更新......

我今天在工作中为此工作了大约 45 分钟,但无法正常工作。我尝试的第一件事是在密码中添加引号,但它仍然不起作用。我刚刚在家里测试了它,它运行良好....... 工作的操作系统是 Windows XP,我在家里运行的是 Windows 7。我会在早上再试一次,以确保我没有打错任何东西。这是我为使其在 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
4

1 回答 1

0

问题在于使用 & 符号,而不是插入符号:单个 & 符号用作命令分隔符,因此 &-sign 之后的行的其余部分被 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
'
于 2014-05-01T20:34:43.997 回答