使用VBScript将Caret传递给命令行

时间:2013-01-24 04:40:21

标签: vbscript cmd caret

很难搞清楚这个。我有一个要求用户名和密码的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

如果密码中没有插入符号,则此方法可以正常工作。但如果它有一个,例如,如果密码是“通过”(*& ^%$#@!)我从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文件中工作。

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

UPDATE ......

我今天在工作中工作了大约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

1 个答案:

答案 0 :(得分: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
'