NSIS - 当 RequestExecutionLevel admin 不够用时,如何将 admin 级别继承给 ExecShell?

NSIS - How to inherit admin level to ExecShell when RequestExecutionLevel admin is not enough?

在 NSIS 中,我想执行一个 cmd/batch 文件,该文件仅为没有管理员权限的本地用户创建桌面快捷方式。

问题是,主 cmd 命令 (query.exe) 正在运行,如果 cmd 是 运行 作为管理员,而在 NSIS 中尽管 RequestExecutionLevel admin ExecShell 没有继承足够的权限,因为我得到的错误消息是 "query cannot be found"。如果 cmd 运行 没有管理员权限,就会发生这种情况。

有人知道我在执行cmd文件时做错了什么吗?或者有人知道比 cmd 文件更好的方法来为用户桌面创建快捷方式吗?

提前感谢您的帮助。

这是命令文件:

for /f "tokens=1 delims=> " %%a in ('query user ^| findstr /C:"console"') do SET USERNAME=%%a
mklink "C:\users\%USERNAME%\DesktopZip Filemanager" "%programfiles%-zipzFM.exe"

这是我的 NSIS 文件:

;------------------------------------
; Creates desktop shortcuts for the local user instead
; for that user that runs this installer (admin).
; ($DESKTOP references to the user that runs this installer)
;------------------------------------

;------------------------------------
;Includes
!include "MUI.nsh"
!include "LogicLib.nsh"

!define MUI_ABORTWARNING # This will warn the user if he exits from the installer.

;------------------------------------
;Pages of installer
!insertmacro MUI_PAGE_WELCOME # Welcome to the installer page.
!insertmacro MUI_PAGE_INSTFILES # Installing page.
!insertmacro MUI_PAGE_FINISH # Finished installation page.

;------------------------------------
;CRC check
CRCCheck On

;------------------------------------
;Language
!insertmacro MUI_LANGUAGE "English"

;------------------------------------
;Execution level
RequestExecutionLevel admin

;------------------------------------
;Check string length
!define STRING_LENGTH ${NSIS_MAX_STRLEN}

;------------------------------------
;Define the source files for the installer
!define MUI_PRODUCT "SHORTCUTS TEST" #Name of application
!define MUI_FILE_SHORTCUTS_CMD "shortcuts.cmd" #Source of further installation files

!define CMD_SHORTCUTS "$INSTDIR${MUI_FILE_SHORTCUTS_CMD}"

;------------------------------------
;Define the destinations
Name "${MUI_PRODUCT}" # Name of the installer (usually the name of the application to install).
OutFile "${MUI_PRODUCT} Installer.exe" # Name of the installer's file.
InstallDir "C:\Test${MUI_PRODUCT}" # Default installing folder ($PROGRAMFILES is Program Files folder).
ShowInstDetails show # This will always show the installation details.

;------------------------------------
;Installer section
Section "Install"

    ;Create directories:
    CreateDirectory $INSTDIR

    ;Add files
    SetOutPath $INSTDIR
    File "${MUI_FILE_SHORTCUTS_CMD}" #Move file

    ;Create shortcuts at user desktop:
    ;(cmd file needs to be run as administrator,
    ; otherwise the shortcut is created in the wrong desktop.)
    ExecShell "open" '${CMD_SHORTCUTS}' ${SW_SHOW}

SectionEnd

;eof

我不认为 query.exe 存在于 Windows 的每个版本中,可能只存在于 Windows Server 和 Pro SKU 中。即使它确实存在于任何地方也是错误的做法,用户可以更改桌面文件夹的路径 and/or 名称,它甚至可能不在配置文件目录中。

它不再是 1995 年了,如果你正在安装到 %ProgramFiles% 并写入 HKEY_LOCAL_MACHINE 那么你正在做一个机器/"all users" 安装,你应该使用 SetShellVarContext All 所以$Desktop 解析为共享桌面文件夹。

mklink 创建一个符号链接,但大多数安装程序应该创建一个 .lnk 快捷方式,您可以在 NSIS 中使用 CreateShortcut.

Windows 徽标指南说您应该只在开始菜单文件夹中创建一个快捷方式,而不要在桌面上创建任何快捷方式!