【问题标题】:NSIS - How to inherit admin level to ExecShell when RequestExecutionLevel admin is not enough?NSIS - 当 RequestExecutionLevel 管理员不够时,如何将管理员级别继承给 ExecShell?
【发布时间】:2017-07-31 11:29:23
【问题描述】:

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

问题是,如果 cmd 以管理员身份运行,则主 cmd 命令 (query.exe) 正常工作,尽管在 NSIS 中 RequestExecutionLevel admin ExecShell 没有继承足够的权限,因为我得到了“找不到查询”的错误消息。如果在没有管理员权限的情况下运行 cmd,就会出现这种情况。

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

提前感谢您的帮助。

这里是 cmd 文件:

for /f "tokens=1 delims=> " %%a in ('query user ^| findstr /C:"console"') do SET USERNAME=%%a
mklink "C:\users\%USERNAME%\Desktop\7Zip Filemanager" "%programfiles%\7-zip\7zFM.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

【问题讨论】:

    标签: cmd exec admin nsis shortcut


    【解决方案1】:

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

    现在已经不是 1995 年了,如果您要安装到 %ProgramFiles% 并写入 HKEY_LOCAL_MACHINE,那么您正在执行机器/“所有用户”安装,您应该使用 SetShellVarContext All,以便 $Desktop 解析为共享桌面文件夹。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多