【问题标题】:VBS to Create Desktop Shortcut with RunAs ArgumentsVBS 使用 RunAs 参数创建桌面快捷方式
【发布时间】:2017-02-17 20:56:19
【问题描述】:

我的目标:我正在尝试使用 VBS 在运行它的当前用户下为 IE 创建一个桌面快捷方式,并使用带有参数的 RunAs 命令和一个变量(InputBox)提示符,该提示符用用户的输入填充变量。

操作系统:Win7 x64

Working 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

【问题讨论】:

  • 你需要正确转义TargetPath中的字符串,当在字符串中使用引号时,双引号是oUrlLink.TargetPath = "%windir%\system32\runas.exe /u:DOMAIN\" & strUser & " ""%ProgramFiles%\Internet Explorer\iexplore.exe"""这样的规则。

标签: vbscript windows-7-x64 runas


【解决方案1】:

目前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

抱歉,重点是错误的问题,虽然这是一个问题,但主要问题是您如何设置 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

【讨论】:

  • 谢谢 Lankymart!似乎仍然有错误...错误:-------------------------- -------------------------------------------------- -- 创建桌面快捷方式 (RunAs Internet Explorer (Doamin Account).vbs(5, 1) Microsoft VBScript runtime error: Invalid procedure call or argument -------------------- -------------------------------------------------- -------------------- 它正在提示输入框字符串,但执行失败。我也尝试用相同的确切错误分隔最后一个空格和参数:(
  • 分隔字符串.... oUrlLink.TargetPath = "%windir%\system32\runas.exe /u:DOMAIN\" & strUser & " " & "%ProgramFiles%\Internet Explorer\iexplore. exe"
  • @GekkoLlama 再次不起作用,因为%ProgramFiles% 将扩展为Program Files,这意味着您需要将其括在引号中(带引号的字符串,请参阅示例),所以按照答案中的建议,应该是oUrlLink.TargetPath = "%windir%\system32\runas.exe /u:DOMAIN\" & strUser & " ""%ProgramFiles%\Internet Explorer\iexplore.exe"""。它在评论中的格式不正确,只需复制我的答案中的行 - “考虑到这一点,该行应该是”
  • 我确实复制并粘贴了您的“考虑到这一点,该行应该是”的答案,这就是错误的来源。我已经在多台机器上尝试过同样的错误。
  • 脚本:设置 WshShell = WScript.CreateObject("WScript.Shell") strDesktop = WshShell.SpecialFolders("Desktop") strUser = InputBox ("请输入您的域帐户") 设置 oUrlLink = WshShell. CreateShortcut(strDesktop & "\RunAs Internet Explorer (strUser).lnk") oUrlLink.TargetPath = "%windir%\system32\runas.exe /u:DOMAIN\" & strUser & """%ProgramFiles%\Internet Explorer\iexplore .exe""" oUrlLink.IconLocation = "%ProgramFiles%\Internet Explorer\iexplore.exe" oUrlLink.Save
猜你喜欢
  • 2013-03-14
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
  • 1970-01-01
  • 2014-01-21
  • 1970-01-01
相关资源
最近更新 更多