2017-02-17 152 views
1

我的目標:我想使用VBS爲當前用戶創建一個桌面快捷方式,使用帶有參數和變量(InputBox)提示符的RunAs命令運行它用戶輸入的變量。VBS創建帶有RunAs參數的桌面快捷方式

OS:X64 Win7的

工作BAT:(在域\用戶名手動填充)

%windir%\system32\runas.exe /u:Domain\Username "%ProgramFiles%\Internet Explorer\iexplore.exe" 

非工作VBS:

set WshShell = WScript.CreateObject("WScript.Shell") 
strDesktop = WshShell.SpecialFolders("Desktop") 
strUser = InputBox ("Please Enter your Domain Account") 
set oUrlLink = WshShell.CreateShortcut(strDesktop & "\RunAs Internet Explorer (Domain Account).lnk") 
oUrlLink.TargetPath = ("%windir%\system32\runas.exe" /u:DOMAIN\"" & strUser & "%ProgramFiles%\Internet Explorer\iexplore.exe") 
oUrlLink.IconLocation = "%ProgramFiles%\Internet Explorer\iexplore.exe" 
oUrlLink.Save 
+0

您需要更正逃避了'TargetPath'字符串,使用字符串引號內的時候,他們的兩倍像'oUrlLink.TargetPath =「%WINDIR統治%\ SYSTEM32 \ runas.exe/U: DOMAIN \「&strUser&」「」%ProgramFiles%\ Internet Explorer \ iexplore.exe「」「''。 – Lankymart

回答

0

此刻的TargetPath不一個有效的字符串,當進入和退出字符串時,他們總是應該以一個雙引號開始和結束。爲了避免「破壞」字符串並導致語法錯誤,字符串中的文字引號也必須被轉義。爲了避免字符串中的字面引號,請將其加倍。

下面是一些應該幫助的例子。

Dim TestString 
TestString = "Simple string" 
'Simple string 
TestString = "Concatenated" & " string" 
'Concatenated string 
TestString = "Another " & TestString & " with a variable" 
'Another Concatenated string with a variable 
TestString = """Quoted string""" 
'"Quoted string" 
TestString = "This is a """ & TestString & """ in a variable" 
'This is a "Quoted string" in a variable 

考慮到這一點,該行應已

oUrlLink.TargetPath = "%windir%\system32\runas.exe /u:DOMAIN\" & strUser & " ""%ProgramFiles%\Internet Explorer\iexplore.exe""" 

輸出爲:

%windir%\system32\runas.exe /u:DOMAIN\Username "%ProgramFiles%\Internet Explorer\iexplore.exe" 

the comments

對不起專注於錯誤問題,whi這是一個問題,主要問題是你如何設置TargetPath。它應該只包含可執行文件的路徑,任何參數需要用Arguments屬性指定,所以試試看。

set WshShell = WScript.CreateObject("WScript.Shell") 
strDesktop = WshShell.SpecialFolders("Desktop") 
strUser = InputBox ("Please Enter your Domain Account") 
set oUrlLink = WshShell.CreateShortcut(strDesktop & "\RunAs Internet Explorer (Domain Account).lnk") 
oUrlLink.TargetPath = "%windir%\system32\runas.exe" 
'Use arguments to pass any arguments for the executable. 
oUrlLink.Arguments = "/u:DOMAIN\" & strUser & " ""%ProgramFiles%\Internet Explorer\iexplore.exe""" 
oUrlLink.IconLocation = "%ProgramFiles%\Internet Explorer\iexplore.exe" 
oUrlLink.Save 
+0

謝謝Lankymart!它似乎仍然有錯誤... 錯誤: ---------------------------------- -------------------------------------------------- ------ 創建桌面快捷方式(RunAs Internet Explorer(Doamin帳戶).vbs(5,1)Microsoft VBScript運行時錯誤:無效的過程調用或參數 -------------- -------------------------------------------------- -------------------------- 它提示輸入框字符串,但在執行失敗。我也試圖分開最後空格和參數具有相同的確切錯誤:( –

+0

分隔字符串.... oUrlLink.TargetPath =「%windir%\ system32 \ runas.exe/u:DOMAIN \」&strUse r&「」&「%ProgramFiles%\ Internet Explorer \ iexplore.exe」 –

+0

@GekkoLlama再次這樣做是不行的,因爲'%ProgramFiles%'會擴展到'Program Files',這意味着你需要用引號括起來* (帶引號的字符串,請參閱示例)*,所以按照答案中的建議,它應該是'oUrlLink.TargetPath =「%windir%\ system32 \ runas.exe/u:DOMAIN \」&strUser&「」「%ProgramFiles %\ Internet Explorer \ iexplore.exe「」「'。它在評論中格式不正確,只是在我的答案中複製該行 - *「考慮到這一點,該行應該是」*「。 – Lankymart

相關問題