我可以让NSIS制作一个处理本地部署和系统部署的安装程序吗?

时间:2016-03-23 18:04:34

标签: windows installer nsis

Chrome有一个安装程序可用于安装系统范围,如果用户不是管理员,则可用于安装到用户的主目录。这对于您在企业环境中进行部署非常有用,在这种情况下您仍然希望允许潜在用户安装,即使他们不一定有权使用。

可以使用NSIS创建这样的安装程序吗?

1 个答案:

答案 0 :(得分:1)

原来它可以。重要的部分是:

  • RequestExecutionLevel highest:这可确保安装程序获得用户帐户可用的最高权限。即如果它们在管理员组中,安装程序将要求提供权限升级。
  • 确定用户是否是管理员。这是使用UserInfo插件实现的。这很简单。
  • SetShellVarContext all|current:这决定了特殊注册表根密钥SHCTX的值。对于all,它表示与HKLM(系统范围)相同,对于current,它会生成HKCUSetShellVarContext也会影响$SMPROGRAMS的值是否指向系统范围的开始菜单或仅用户的层次结构。

这是安装程序的框架,可以在系统范围内或本地部署,具体取决于用户帐户的权限。它使用C:\ Windows \ write.exe作为其有效负载,并可选择安装开始菜单项和桌面快捷方式。它还在注册表中引用了卸载程序,以便它显示在“添加/删除程序”对话框中。我使用NSIS 3.0(测试版)构建了这个,但我没有看到任何明显的原因导致它无法使用最近的2.x.

!include "MUI2.nsh"

!define PRODUCT_NAME "DummyProduct"
!define VERSION "0.0.1"

Var INSTDIR_BASE

Name "${PRODUCT_NAME}"
OutFile "${PRODUCT_NAME} Installer.exe"

InstallDir ""

; Take the highest execution level available
; This means that if it's possible to, we become an administrator
RequestExecutionLevel highest

!macro ONINIT un
    Function ${un}.onInit
        ; The value of SetShellVarContext detetmines whether SHCTX is HKLM or HKCU
        ; and whether SMPROGRAMS refers to all users or just the current user
        UserInfo::GetAccountType
        Pop $0
        ${If} $0 == "Admin"
            ; If we're an admin, default to installing to C:\Program Files
            SetShellVarContext all
            StrCpy $INSTDIR_BASE "$PROGRAMFILES64"
        ${Else}
            ; If we're just a user, default to installing to ~\AppData\Local
            SetShellVarContext current
            StrCpy $INSTDIR_BASE "$LOCALAPPDATA"
        ${EndIf}

        ${If} $INSTDIR == ""
            ; This only happens in the installer, because the uninstaller already knows INSTDIR
            ReadRegStr $0 SHCTX "Software\${PRODUCT_NAME}" ""

            ${If} $0 != ""
                ; If we're already installed, use the existing directory
                StrCpy $INSTDIR "$0"
            ${Else}
                StrCpy $INSTDIR "$INSTDIR_BASE\${PRODUCT_NAME}"
            ${Endif}
        ${Endif}
    FunctionEnd
!macroend

; Define the function twice, once for the installer and again for the uninstaller
!insertmacro ONINIT ""
!insertmacro ONINIT "un"

!define MUI_ABORTWARNING

!define MUI_COMPONENTSPAGE_NODESC
!insertmacro MUI_PAGE_COMPONENTS

!insertmacro MUI_PAGE_DIRECTORY

Var STARTMENU_FOLDER
!define MUI_STARTMENUPAGE_REGISTRY_ROOT "SHCTX"
!define MUI_STARTMENUPAGE_REGISTRY_KEY "Software\${PRODUCT_NAME}"
!define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "Start Menu Folder"
!insertmacro MUI_PAGE_STARTMENU ${PRODUCT_NAME} $STARTMENU_FOLDER

!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

!insertmacro MUI_LANGUAGE "English"

Section "-Main Component"
    SetOutPath "$INSTDIR"

    File "C:\Windows\write.exe"

    WriteRegStr SHCTX "Software\${PRODUCT_NAME}" "" $INSTDIR

    ; These registry entries are necessary for the program to show up in the Add/Remove programs dialog
    WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "DisplayName" "${PRODUCT_NAME}"
    WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "UninstallString" '"$INSTDIR\Uninstall.exe"'
    WriteRegDWORD SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "NoModify" 1
    WriteRegDWORD SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "NoRepair" 1

    WriteUninstaller "$INSTDIR\Uninstall.exe"

    !insertmacro MUI_STARTMENU_WRITE_BEGIN ${PRODUCT_NAME}
        CreateDirectory "$SMPROGRAMS\$STARTMENU_FOLDER\"
        CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\${PRODUCT_NAME}.lnk" "$INSTDIR\write.exe"
    !insertmacro MUI_STARTMENU_WRITE_END
SectionEnd

Section "Desktop shortcut"
    CreateShortCut "$DESKTOP\${PRODUCT_NAME}.lnk" "$INSTDIR\write.exe"
SectionEnd

Section "Uninstall"
    Delete "$INSTDIR\write.exe"

    Delete "$INSTDIR\Uninstall.exe"

    RMDir /r "$INSTDIR"

    !insertmacro MUI_STARTMENU_GETFOLDER ${PRODUCT_NAME} $STARTMENU_FOLDER
    Delete "$SMPROGRAMS\$STARTMENU_FOLDER\${PRODUCT_NAME}.lnk"
    RMDir /r "$SMPROGRAMS\$STARTMENU_FOLDER"

    Delete "$DESKTOP\${PRODUCT_NAME}.lnk"

    DeleteRegKey /ifempty SHCTX "Software\${PRODUCT_NAME}"

    DeleteRegKey SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
SectionEnd