Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Com 如何使用JScript创建使用“的快捷方式?”;“以管理员身份运行”;_Com_Activex_Shortcut_Jscript_Wsh - Fatal编程技术网

Com 如何使用JScript创建使用“的快捷方式?”;“以管理员身份运行”;

Com 如何使用JScript创建使用“的快捷方式?”;“以管理员身份运行”;,com,activex,shortcut,jscript,wsh,Com,Activex,Shortcut,Jscript,Wsh,我有一个使用cscript.exe运行的JScript脚本。它在桌面上(以及“开始”菜单中)创建一个快捷方式,该快捷方式使用参数运行cscript.exe,以运行另一个JScript脚本。在相关部分,它看起来像这样: function create_shortcut_at(folder, target_script_folder) { var shell = new ActiveXObject("WScript.Shell"); var shortcut = shell.Crea

我有一个使用
cscript.exe
运行的JScript脚本。它在桌面上(以及“开始”菜单中)创建一个快捷方式,该快捷方式使用参数运行
cscript.exe
,以运行另一个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\u shortcut\u at(桌面文件夹、脚本文件夹)

就目前而言,这是可行的。它创建桌面图标,正确指向脚本,并在双击时运行它。问题是它确实需要以“管理员”身份运行脚本

而且脚本确实需要以“管理员”身份运行——它安装应用程序(适用于所有用户)并重新启动计算机。(对于感兴趣的人,脚本是wpkg.js。不希望将其修改为自提升。)

因为快捷方式的目标实际上是“cscript.exe”,所以我不能使用清单进行升级。理论上,我可以在windows目录中安装cscript.exe.manifest,但即使这样做有效,出于显而易见的原因,这将是一个糟糕的想法

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

三十秒的调查表明WScript.Shell ActiveX对象没有实现此目的所需的接口。其他调查表明IShellLinkDataList确实如此。但是,IShellLinkDataList是一个通用COM接口。我在网上看到了几个例子,大多数都是链接。但是,所有示例都是在编译代码(C++、C#甚至JScript.NET)中完成的。我非常希望能够直接在JScript中执行,从
cscript.exe
运行


也就是说,我完全支持我没有想到的想法或其他解决方案。

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

但是,如果您对一个hack感到满意,您可以在脚本中完成它,只需在.lnk文件中翻转一点即可

当您在Shell属性框的高级选项卡中勾选“以管理员身份运行”框时,或者当您要包括
SLDF\u RUNAS\u USER
时,您基本上只需在文件中设置一位

您可以“手动”完成此操作,而无需通过COM接口。它是字节21,您需要将0x20位设置为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));

为帮助未来需要此解决方案的人员,此处指定了.lnk格式:。特别是,这使用了。非常聪明的解决方案,谢谢!
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();
}