2012-06-22 39 views
6

我有一个使用cscript.exe运行的JScript脚本。它在桌面(和开始菜单)上创建一个运行cscript.exe并带有参数的快捷方式,以运行另一个JScript脚本。看起来,在相关部分,像这样:如何使用JScript创建使用“以管理员身份运行”的快捷方式

function create_shortcut_at(folder, target_script_folder) 
{ 
    var shell = new ActiveXObject("WScript.Shell"); 
    var shortcut = shell.CreateShortcut(folder + "\\Run The Script.lnk"); 
    shortcut.TargetPath = "cscript"; 
    shortcut.Arguments = "\""+target_script_folder+"\\script.js\" /aParam /orTwo"; 
    shortcut.IconLocation = target_script_folder+"\\icon.ico"; 
    shortcut.Save(); 
} 

它被调用像create_shortcut_at(desktop_folder, script_folder)

而且,尽可能地发挥作用。它创建桌面图标,正确指向脚本并在双击时运行它。问题在于它确实需要以“管理员身份”运行脚本。

而且脚本确实需要以“管理员”身份运行 - 它会安装应用程序(针对所有用户)并重新启动计算机。 (对于那些感兴趣的人,脚本是wpkg.js.修改它以自我提升是不可取的。)

由于快捷方式的目标实际上是“cscript.exe”,因此我无法使用清单进行升级。我理论上可能会在Windows目录中安装一个cscript.exe.manifest,但即使这样做,这显然是一个可怕的理由。

我也不想使用虚拟脚本,因为这是一个额外的文件来处理,还有另一个看似合理的解决方案:检查快捷方式上的“以管理员身份运行”框。

三十秒的调查显示WScript.Shell ActiveX对象没有所需的接口。额外的调查表明,IShellLinkDataList可以。但是,IShellLinkDataList是一个通用的COM接口。我在互联网上看到了几个例子,大多数连接here。但是,所有示例都是以编译代码(C++,C#,甚至是JScript.NET)来完成的。我非常希望能够直接使用JScript,从cscript.exe运行。

这就是说,我都为我没有考虑过的想法或其他解决方案。

回答

4

将快捷方式文件标记为需要提升的官方方法是通过IShellLinkDataList。从自动化环境中使用该接口很困难。

但是,如果您对破解感到高兴,您可以在脚本中完成,只需在.lnk文件中翻转一下即可。

当您在“壳牌属性”框的“高级”选项卡中勾选“以管理员身份运行”框时,或者当您将use IShellLinkDataList to set the flags包括在SLDF_RUNAS_USER中时,基本上只需在文件中设置一位。

您可以在不通过COM接口的情况下“手动”执行此操作。它是字节21,你需要设置0x20位。

(function(globalScope) { 
    'use strict'; 
    var fso = new ActiveXObject("Scripting.FileSystemObject"), 
     path = "c:\\path\\goes\\here\\Shortcut2.lnk", 
     shortPath = path.split('\\').pop(), 
     newPath = "new-" + shortPath; 

    function readAllBytes(path) { 
     var ts = fso.OpenTextFile(path, 1), a = []; 
     while (!ts.AtEndOfStream) 
      a.push(ts.Read(1).charCodeAt(0)); 
     ts.Close(); 
     return a; 
    } 

    function writeBytes(path, data) { 
     var ts = fso.CreateTextFile(path, true), 
      i=0, L = data.length; 
     for (; i<L; i++) { 
      ts.Write(String.fromCharCode(data[i])); 
     } 
     ts.Close(); 
    } 

    function makeLnkRunAs(path, newPath) { 
     var a = readAllBytes(path); 
     a[0x15] |= 0x20; // flip the bit. 
     writeBytes(newPath, a); 
    } 

    makeLnkRunAs(path, newPath); 

}(this)); 

PS:

function createShortcut(targetFolder, sourceFolder){ 
    var shell = new ActiveXObject("WScript.Shell"), 
     shortcut = shell.CreateShortcut(targetFolder + "\\Run The Script.lnk"), 
     fso = new ActiveXObject("Scripting.FileSystemObject"), 
     windir = fso.GetSpecialFolder(specialFolders.windowsFolder); 

    shortcut.TargetPath = fso.BuildPath(windir,"system32\\cscript.exe"); 
    shortcut.Arguments = "\"" + sourceFolder + "\\script.js\" /aParam /orTwo"; 
    shortcut.IconLocation = sourceFolder + "\\icon.ico"; 
    shortcut.Save(); 
} 
+0

为了帮助谁需要这个解决方案未来的人,在这里指定的.LNK格式:http://msdn.microsoft.com/en-us/library/dd871305( v = prot.13)。特别是,它使用http://msdn.microsoft.com/en-us/library/dd891314(v=prot.13)。非常聪明的解决方案,谢谢! – alficles

相关问题