NSIS - 如果RequestExecutionLevel管理员不够,如何将管理级别继承到ExecShell?

时间:2017-07-31 11:29:23

标签: cmd exec admin nsis shortcut

NSIS中的

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

问题是,主cmd命令(query.exe)正常工作,如果cmd以admin身份运行,而且在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

1 个答案:

答案 0 :(得分:0)

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

不再是1995年,如果您要安装到%ProgramFiles%并写入HKEY_LOCAL_MACHINE,那么您正在安装一台机器/“所有用户”,您应该使用SetShellVarContext All以便$Desktop结算到共享桌面文件夹。

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

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