2013-01-24 33 views
2

難以捉摸這一個。我有一個vbscript,要求輸入用戶名和密碼。然後腳本運行PSExec.exe,將這些傳遞到命令行。傳遞使用VBScript命令行插入符號

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看到的%$#@!作爲命令。

我的空間在使用批處理文件時看到了一個傳遞它的例子,但在這種情況下它不起作用xtra ^像這樣^^在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

從本評論結尾的SO回答鏈接需要4個插文:http://stackoverflow.com/a/5254713/1569434 – Lizz

回答

0

使用符號的問題奠定了,不脫字符:單個號用作命令分隔符,所以&後,該行的其餘部分 - 標誌被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 
'