如何使用JScript创建一个使用“以管理员身份运行”的快捷方式 [英] How can I use JScript to create a shortcut that uses "Run as Administrator"

查看:339
本文介绍了如何使用JScript创建一个使用“以管理员身份运行”的快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a JScript script that runs using cscript.exe. It creates a shortcut on the desktop (and in the start menu) that runs cscript.exe with parameters to run another JScript script. It looks, in relevant part, like this:

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)

而且行之有效。它创建桌面图标,正确指向该脚本,并在双击时运行它。问题是它真的需要以管理员身份运行脚本。

And that works, as far as it goes. It creates the desktop icon, pointing properly to the script and runs it when double-clicked. The problem is that it really needs to run the script "as administrator".

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

And the script really does need to run "as administrator" -- it installs applications (for all users) and reboots the computer. (For those interested, the script is wpkg.js. Modifying it to self-elevate is undesirable.)

由于快捷方式的目标实际上是cscript.exe ,我不能使用清单来升级。我可能理论上在Windows目录中安装一个cscript.exe.manifest,但即使这样工作,这将是一个可怕的想法,因为很明显。

Since the target of the shortcut is actually "cscript.exe", I can't use a manifest for the escalation. I could probably theoretically install a cscript.exe.manifest in the windows directory, but even if that worked, it would be a terrible idea for reasons that are obvious.

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

I'd also prefer not to use a dummy script, since that is an extra file to deal with and there's another, seemingly reasonable, solution at hand: check the "Run as administrator" box on the shortcut.

三十秒的调查显示,WScript.Shell ActiveX对象没有所需的接口。额外的调查表明IShellLinkDataList。但是,IShellLinkDataList是一个通用COM接口。我在互联网上看到几个例子,大多数链接这里。然而,所有的例子在编译代码(C ++,C#,甚至JScript.NET)。我显然更喜欢能够直接在JScript中运行,从 cscript.exe 运行。

Thirty-seconds of investigation reveals that the WScript.Shell ActiveX Object does not have the interfaces required for this. Additional investigation suggests that IShellLinkDataList does. However, IShellLinkDataList is a generic COM Interface. I see several examples around the Internet, most linking here. However, all the examples do it in compiled code (C++, C#, even JScript.NET). I significantly prefer to be able to do it directly in JScript, running from cscript.exe.

推荐答案

将快捷文件标记为需要elevation是通过 IShellLinkDataList 。很难从自动化环境中使用该接口。

The official way to mark a shortcut file as requiring elevation is via IShellLinkDataList. It's difficult to use that interface from an automation environment.

但是,如果你对一个黑客感到满意,你可以在脚本中做,只需在.lnk文件中翻转一下。

But, if you are happy with a hack, you can do it in script, just by flipping a bit in the .lnk file.

当您在Shell属性框的高级选项卡中勾选以管理员身份运行框时,或者当您使用IShellLinkDataList设置标志以包括 SLDF_RUNAS_USER ,你基本上只是在文件中设置一个位。

When you tick the "run as administrator" box in the Advanced tab of the Shell Properties box, or when you use IShellLinkDataList to set the flags to include SLDF_RUNAS_USER, you're basically just setting one bit in the file.

您可以手动而不通过COM界面。它是字节21,你需要设置0x20位。

You can do that "manually" without going through the COM interface. It's byte 21, and you need to set the 0x20 bit on.

(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();
}

这篇关于如何使用JScript创建一个使用“以管理员身份运行”的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆