VBS创建带有RunAs参数的桌面快捷方式

问题描述:

我的目标:我想使用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

此刻的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