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

查看:34
本文介绍了如何使用 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).

It gets called like 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.不希望将其修改为 self-elevate.)

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.

我也不想使用虚拟脚本,因为这是要处理的额外文件,而且手头还有另一个看似合理的解决方案:选中快捷方式上的以管理员身份运行"框.

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.

30 秒的调查表明 WScript.Shell ActiveX 对象没有为此所需的接口.其他调查表明 IShellLinkDataList 确实如此.但是,IShellLinkDataList 是一个通用的 COM 接口.我在互联网上看到几个例子,大多数链接 here.但是,所有示例都是在编译后的代码(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.

也就是说,我完全赞成我没有考虑过的想法或其他解决方案.

That said, I'm all for ideas I didn't contemplate or other solutions.

推荐答案

将快捷方式文件标记为需要提升的官方方法是通过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.

但是,如果您对 hack 感到满意,您可以在脚本中完成,只需在 .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天全站免登陆