Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How-to: Building an unattended installer and uninstaller using AutoHotkey and pushing to Chocolatey repository

License

NotificationsYou must be signed in to change notification settings

wandersick/az-autohotkey-silent-setup

Repository files navigation

Supposed there is a portable Windows application without an installer and uninstaller, how to create them back? In today's post, we will explore one way to build aSetup.exe using AutoHotkey (AHK), with additional compression of 7-Zip applied to theSetup.exe and remaining files of the portable application for maximum compression, and then wrap it with an outer unattended installer, turning a portable application into an installable one while being suitable also for silent deployment.

The application example, i.e. the application for which a setup is created isAeroZoom. While some terminologies are specific to AeroZoom, the general concepts should apply to other software.

// ⭐1️⃣ to be built: outer unattended installer written in AHKAeroZoom_Unattended_Installer.exe    │    │// 2️⃣ to be built: 7-Zip self-extracting archive    │    └───AeroZoom_7-Zip_SFX.exe          │          ├───AeroZoom// portable app example          │          │AeroZoom.exe          │      ...          │          │// ⭐3️⃣ to be built: inner setup written in AHK          │          │Setup.exe          │          └───Data
  1. AeroZoom_Unattended_Installer.exe will be the outer unattended installer we will write inAutoHotkey (AHK) language, which contains 7-Zip SFXAeroZoom_7-Zip_SFX.exe and is responsible for extraction as well as calling an innerSetup.exe to install AeroZoom for all users silently

  2. The second-levelAeroZoom_7-Zip_SFX.exe will be our7-Zip self-extracting archive (SFX). It contains the portable application files of AeroZoom and the innerSetup.exe

    • Use of a 7-Zip SFX automatically provides silent extraction parameters for the outerAeroZoom_Unattended_Installer.exe to leverage
    • The SFX will be zipped with the ultra compression option to effectively reduce the size of AeroZoom from 32MB to 2MB in the case of AeroZoom which contains multiple similar executables
  3. The third-levelSetup.exe, the inner setup, is the second AutoHotkey program we will write which provides installations parameters for the outerAeroZoom_Unattended_Installer.exe which can be leveraged e.g. bychoco install command of Chocolatey, uninstallation parameters forchoco uninstall, as well as logic to determine several things such as whether AeroZoom has already been installed, which will be detailed in the last section

After walking through how to createAeroZoom_7-Zip_SFX.exe andAeroZoom_Unattended_Installer.exe (goal #1), we will take a detour to go through how to push this unattended installer to the community repository ofChocolatey (goal #2), the package manager for Windows, before going back to detailing how to create the innerSetup.exe (goal #3).

Installing AeroZoom via Chocolatey community repository (goal #2)

This how-to will be centered around our code repository on GitHub:

Table of Content

Let's go and create all those exe files above!

Some Trivia of AeroZoom (before We Begin)

Scripted in AHK,AeroZoom enhances upon Windows Magnifier and optionally Sysinternals ZoomIt to enable screen magnification by mouse-wheeling, as well as turning any mouse into a Home-Theater PC/presentation mouse, where zooming and positioning becomes a breeze without a keyboard.

Originally, AeroZoom was built as a portable application. ItsSetup.exe was introduced in a later version,v2.0, and the unattended setupAeroZoom_Unattended_Installer.exe, the first outcome of this article, was introduced inv4.0.

1. Step-by-Step: Building Outer Unattended Installer Using AutoHotkey

This first section walks through how to create the outer unattended installer,AeroZoom_Unattended_Installer.exe, written in AutoHotkey:

  1. In Command Prompt, download orgit cloneour code repository to a desired directory e.g.c:\az-autohotkey-silent-setup

    cd /d c:git clone https://github.com/wandersick/az-autohotkey-silent-setup.git

    So that the following folder structure is acquired

    C:\az-autohotkey-silent-setup│   ...││// AHK code of the outer unattended installer to be customized││AeroZoom_Unattended_Installer.ahk││// optional: for use with Compile_AHK II (alternative compiler)│└───AeroZoom_Unattended_Installer.ahk.ini
  2. DownloadAeroZoom and extract it to a desired directory, e.g.C:\AeroZoom

    C:\AeroZoomAeroZoom.exeReadme.txtSetup.exe│└───Data
  3. Build an innerSetup.exe using AutoHotkey followingthese instructions in the third and final section to learn the process, or leave it as is for now and learn how to build it later (recommended for now)

    • EnsureSetup.exe is next to AeroZoom
    C:\AeroZoomAeroZoom.exeReadme.txt││// if you manually build this Setup.exe, replace existing one// otherwise, no action is required││Setup.exe│└───Data
  4. Package (compress) the aboveC:\AeroZoom application directory in a7-Zip SFX (self-extracting archive)

    • Optionally, this step can be skipped by leveraging the AeroZoom download, which already comes with an SFXAeroZoom_v4.0.0.7_beta_2.exe after extraction

    • Put the SFX file there and rename it asAeroZoom_7-Zip_SFX.exe

    C:\az-autohotkey-silent-setupAeroZoom_Unattended_Installer.ahkAeroZoom_Unattended_Installer.ahk.iniREADME.md│   ...││// place SFX (containing C:\AeroZoom) built using 7-Zip here│└───AeroZoom_7-Zip_SFX.exe
  5. Place an icon namedAeroZoom_Setup.ico there (optional)

    C:\az-autohotkey-silent-setupAeroZoom_7-Zip_SFX.exeAeroZoom_Unattended_Installer.ahkAeroZoom_Unattended_Installer.ahk.iniREADME.md│   ...││// optionally, place an icon here│└───AeroZoom_Setup.ico
  6. EditAeroZoom_Unattended_Installer.ahk and change belowC:\az-autohotkey-silent-setup\AeroZoom_7-Zip_SFX.exe to a desired location (no change if directory is the same as the example)

    As shown below, the AutoHotkey source code ofAeroZoom_Unattended_Installer.exe is relatively simple. It only contains three lines (excluding comments):

    ; Package an application (e.g. AeroZoom) in 7-Zip SFX; (FYI: the AeroZoom download already comes with an SFX); Place it in the location specified below; e.g. C:\az-autohotkey-silent-setup\AeroZoom_7-Zip_SFX.exeFileInstall, C:\az-autohotkey-silent-setup\AeroZoom_7-Zip_SFX.exe, %A_ScriptDir%\AeroZoom_7-Zip_SFX.exe,1; Silently extract AeroZoom from SFX into the current folderRunWait, %A_ScriptDir%\AeroZoom_7-Zip_SFX.exe-o"%A_ScriptDir%"-y; Run silent setup command: Setup.exe /programfiles /unattendaz=1; For AeroZoom, thse parameters will perform installation:; - silently (/unattendedaz=1); - to All Users (/programfiles); Or uninstall in case AeroZoom is found in the target folder; (built into the logic of Setup.exe of AeroZoom)RunWait, %A_ScriptDir%\AeroZoom\Setup.exe/programfiles/unattendaz=1
  7. Download and install AutoHotKey (specify 32-bit in the setup wizard to maximize compatibility of the compiled executables)

  8. To compile the AutoHotkey script as an executable, download and installCompile_AHK II.

    • While under repository directory (e.g.C:\az-autohotkey-silent-setup), right-clickAeroZoom_Unattended_Installer.ahk and selectCompile with Options which would parse parameters fromAeroZoom_Unattended_Installer.ahk.ini
    • The ini file has an option namedExecution_Level=4 which meansrequireAdministrator (run as administrator). This is required for the installer to carry out installation successfully.
    • While Compile_AHK II comes with compression feature, this article uses 7-Zip as 7-Zip reduces the file size much better (from 32MB to 2MB) in our case.

    Alternatively, the default compiler which comes with AutoHotkey can be used instead of Compile_AHK II if preferred.

    • While under repository directory (e.g.C:\az-autohotkey-silent-setup), compileAeroZoom_Unattended_Installer.ahk using the bundled AHk2Exe utility, usually located underC:\Program Files\AutoHotkey\Compiler as so:
    • "C:\Program Files\AutoHotkey\Compiler\Ahk2Exe.exe" /in "AeroZoom_Unattended_Installer.ahk" /icon "AeroZoom_Setup.ico"
      • Icon parameter is optional:/icon "AeroZoom_Setup.ico"
    • For me, I prefer Compile_AHK II than the official compiler in our case where the installer requires “Run as Administrator” rights. Compile_AHK II has built in support ofExecution_Levels while the official compiler hasother means to achieve it.

    To each their own, choose a method with which you are comfortable. If you know of a better way, feel free to let me know!

  9. Done. Now executingAeroZoom_Unattended_Installer.exe would silently trigger an extraction of 7-Zip SFXAeroZoom_7-Zip_SFX.exe and calls the inner AeroZoomSetup.exe to install AeroZoom for all users with its unattended installation parameter/programfiles /unattendAZ=1 (which will be further explained in the third and final section)

Goal #1: The end result,AeroZoom_Unattended_Installer.exe, is available fordownload here (asAeroZoom_v4.0.0.7_beta_2_silent_installer.exe).

About False-Positive Notices from Anti-virus Software

Like certain AutoHotkey applications, some anti-virus vendors may falsely identify the unattended installer created as undesirable. It is understandable considering the nature of this application is to perform installation and uninstallation in a silent way, which involves seemingly risky tasks such as modifying uninstallation keys in the registry.

On the other hand, I found that compiling using Compile_AHK II and/or an icon helps lower the detection rates a little bit (from12 down to<10 out of 65 anti-virus applications on VirusTotal).

Still, this false-positive detection issue may not be completely eliminated and can be safely ignored. It is recommended toreport your file to your anti-virus vendor as clean if possible.

2. Pushing Unattended Installer to Chocolatey

Thanks to thisChocolatey how-to article by Coffmans andguidance from Chocolatey, I was able to figure out how to push the outer unattended installer to theChocolatey community repository here.

The Chocolatey package specification files customized for AeroZoom have been included in thegit repository downloaded in step #1 of the above section which was initially created withchoco new <package name> command after Chocolatey has beendownloaded and installed on a supported Windows machine.

Let's walk through the folder structure:

C:\az-autohotkey-silent-setup\Chocolatey\AeroZoomReadMe.md_TODO.txt│   ...││// 1️⃣ package metadata││aerozoom.nuspec│└───tools// 2️⃣ tasks run before [un]install scriptschocolateybeforemodify.ps1// 3️⃣ edited Chocolatey install script//    (for use with 'choco install')chocolateyinstall.ps1// 4️⃣ edited Chocolatey uninstall script//    (for use with: 'choco uninstall')chocolateyuninstall.ps1
  1. aerozoom.nuspec contains the below self-describing package metadata (click the hyperlink to see the AeroZoom example with comments)

    • id
    • version
    • packageSourceUrl
    • owners
    • title
    • authors
    • projectUrl
    • iconUrl
    • licenseUrl
    • requireLicenseAcceptance
    • projectSourceUrl
    • docsUrl
    • mailingListUrl
    • bugTrackerUrl
    • tags
    • summary
    • description
    • releaseNotes
  2. chocolateybeforemodify.ps1 performs pre-checks prior to installation and uninstallation. For example, to prevent running processes from conflicting with setup, we can use PowerShell cmdletStop-Process to forcefully terminate processes

  • It is important to add-ErrorAction SilentlyContinue so that users do not see error texts when the command runs without any process to stop

    Stop-Process-ProcessName aerozoom*-Force-ErrorAction SilentlyContinue
  1. chocolateyinstall.ps1 performs silent installation by downloading and using an unattended installer, which is the outcome from the first section of this article. It should be hosted on a publicly accessible web server in order for this Chocolatey installation script to leverage

    $url='https://github.com/wandersick/aerozoom/releases/download/4.0.2/AeroZoom_v4.0.0.7_beta_2_silent_installer.exe'
  2. chocolateyuninstall.ps1 performs silent uninstallation by using a parameter specific to the application installer, which is/programfiles /unattendAZ=1 in the case of AeroZoom. (The parameters will be further explained in the third and final section)

    silentArgs="/programfiles /unattendAZ=1"

One AeroZoom-specific side-note:

  • Due to the waySetup.exe of AeroZoom works, forchocolateyuninstall.ps1, I have addedSubstring (from PowerShell) to remove/programfiles /unattendAZ=2 fromUninstallString (in Windows registry), replacing it with/programfiles /unattendAZ=1 from$silentArgs (inchocolateyuninstall.ps1). Otherwise, uninstallation would not be performed in an unattended way

    $packageArgs['file']="$($_.UninstallString)".Substring(0,$_.UninstallString.IndexOf(' /'))

Once the PowerShell scripts have been edited, below are the commands for packaging, testing (installing and uninstalling), as well as pushing to the Chocolatey community repository

# create NuGet package (aerozoom.4.0.0.7.nupkg)# ensure current directory is correctcd az-autohotkey-silent-setup\Chocolatey\AeroZoomchoco pack# install aerozoom.4.0.0.7.nupkg by running:# - chocolateybeforemodify.ps1# - chocolateyinstall.ps1choco install aerozoom.4.0.0.7.nupkg# uninstall aerozoom by running:# - chocolateybeforemodify.ps1# - chocolateyuninstall.ps1choco uninstall aerozoom# push to community repo (--api-key required)choco push aerozoom.4.0.0.7.nupkg

Goal #2: Having gone through themoderation review process, AeroZoom is available onChocolatey community repository now.

3. Step-by-Step: Building Inner Setup Using AutoHotkey

This final section walks through how to create the innerSetup.exe acquired after 7-Zip extraction.

The steps for building theSetup.exe would be:

  1. First, let's deleteSetup.exe in the AeroZoom folder

    If not already done so, the files below can bedownloaded here and extracted to a desired directory, e.g.C:\AeroZoom

    C:\AeroZoomAeroZoom.exeReadme.txt│   ...││// Setup.exe will be built using below method here│└───Data
  2. Acquire the source code ofSetup.exe, i.e.Setup.ahk

    If not already done so, open Command Prompt, download orgit cloneour code repository to a desired directory e.g.c:\az-autohotkey-silent-setup

    cd /d c:git clone https://github.com/wandersick/az-autohotkey-silent-setup.git

    So that the following folder structure is acquired

    C:\az-autohotkey-silent-setupSetup.ahk.ini│   ...││// source code of Setup.exe of AeroZoom│└───Setup.ahk
  3. If not already done so,download and install AutoHotKey (specify 32-bit in the setup wizard to maximize compatibility of the compiled executables)

  4. To compile the AutoHotkey script into an executable, download and installCompile_AHK II.

    • While under repository directory (e.g.C:\az-autohotkey-silent-setup), right-clickSetup.ahk and selectCompile with Options, which would parse parameters fromSetup.ahk.ini
    • The ini file containsExecution_Level=3 which meanshighestAvailable (run with highest privileges available) instead ofrequireAdministrator (run as administrator) as theSetup.ahk supports dynamically installing into current user profile (when no administrator rights are available) or installing into system directory e.g.C:\Program Files (x86) (when administrator rights are available andSetup.exe /programfiles is specified)
  5. Done

Goal #3: The end result,Setup.exe, can be used to install and uninstall AeroZoom, provided that it is located in the below directory, and that suitable unattended installation parameters are passed (seeSetup.exe /?), or the installation is performed in an normal attended way (i.e. double-clicking theSetup.exe and follow the on-screen installation instructions).

C:\AeroZoomAeroZoom.exeReadme.txt│   ...││// Place the newly created Setup.exe in this folder// It can install and uninstall AeroZoom hereSetup.exe│└───Data

If you would like, you may also continue from step 3 of the first section to leverage this newly createdSetup.exe to create the outer unattended installer again, which enables a totally silent installation experience of AeroZoom without the need to supply any parameters.

How It Works

Instead of using a popular approach such as Windows Installer, AeroZoom implementsSetup.exe from the ground up, which has its own parameters:

  • /programfiles for installing intoC:\Program Files (x86) (orC:\Program Files for 32-bit OS) instead of current user profile, i.e.%localappdata%, usuallyC:\Users\{username}\AppData\Local
  • /unattendAZ=1 for an unattended installation

If it detects AeroZoom is already installed, the setup will perform an uninstallation instead

  • If/unattendAZ=2 or any other values, an uninstallation dialog box will also be prompted
targetDir=%localappdata%If %1% {  ; For each parameterLoop, %0%  {    ; Retrieve A_Index variable contents    param := %A_Index%If (param="/unattendAZ=1")      unattendAZ=1Else if (param="/unattendAZ=2")      unattendAZ=2Else if (param="/programfiles")    {      targetDir=%programfiles%      setupAllUsers=1    }Else    {Msgbox,262192, AeroZoom Setup, Supported parameters:`n`n- Unattended setup :/unattendAZ=1`n- Install forall users :/programfiles`n`nFor example: Setup.exe/programfiles/unattendaz=1`n`nNote:`n- If setup finds a copy in the target location, uninstallation will be carried out instead.`n- If you install into Program Files folder, be sure you're running it with administrator rights.ExitApp,5    }  }}

During installation or uninstallation,Setup.exe would first check a few prerequisites, such as whether files it depends on exist or not, as well as terminating any executables that could be running.

Once it is OK to proceed with installation,Setup.exe would create shortcuts:

IfExist, %targetDir%\wandersick\AeroZoom\AeroZoom.exe{  ; Create shortcut to Start Menu (All Users)If setupAllUsers  {FileCreateShortcut, %targetDir%\wandersick\AeroZoom\AeroZoom.exe, %A_ProgramsCommon%\AeroZoom.lnk, %targetDir%\wandersick\AeroZoom\,, AeroZoom`, the smooth wheel-zooming and snippingmouse-enhancing panel,,FileCreateShortcut, %targetDir%\wandersick\AeroZoom\AeroZoom.exe, %A_DesktopCommon%\AeroZoom.lnk, %targetDir%\wandersick\AeroZoom\,, AeroZoom`, the smooth wheel-zooming and snippingmouse-enhancing panel,,  }  ; Create shortcut to Start Menu (Current User)Else  {FileCreateShortcut, %targetDir%\wandersick\AeroZoom\AeroZoom.exe, %A_Programs%\AeroZoom.lnk, %targetDir%\wandersick\AeroZoom\,, AeroZoom`, the smooth wheel-zooming and snippingmouse-enhancing panel,,FileCreateShortcut, %targetDir%\wandersick\AeroZoom\AeroZoom.exe, %A_Desktop%\AeroZoom.lnk, %targetDir%\wandersick\AeroZoom\,, AeroZoom`, the smooth wheel-zooming and snippingmouse-enhancing panel,,  }}

Next,Setup.exe would write uninstallation information such asUninstallString to the Windows Registry:

If setupAllUsers{  ; If AeroZoom was installed for all usersRegWrite,REG_SZ,HKEY_CURRENT_USER, %regKey%, UninstallString, %targetDir%\wandersick\AeroZoom\Setup.exe/unattendAZ=2/programfiles}Else{  ; If AeroZoom was installed for current userRegWrite,REG_SZ,HKEY_CURRENT_USER, %regKey%, UninstallString, %targetDir%\wandersick\AeroZoom\Setup.exe/unattendAZ=2}

Other information it writes there are:

  • Publisher
  • DisplayVersion
  • InstallLocation
  • URLInfoAbout
  • HelpLink
  • InstallDate
  • DisplayName
  • DisplayIcon
  • EstimatedSize

Regarding uninstallation, it is simply the above process in reverse, i.e. deleting shortcuts and removing the registry entries.

One thing worth mentioning is previous version of AeroZoom installer (v4.0 and before) would leaveSetup.exe under the installation directory. Sincev5.0, this has been solved by calling a minimizedcmd.exe /c toping localhost followed by the self-destruction, in order forSetup.exe to delete itself.

; Remove directory content including Setup.exe that is running; after 3 seconds using ping in a minimized Command PromptRun, cmd/c start/min ping127.0.0.1-n3 >nul& rd/s/q"%targetDir%\wandersick\AeroZoom"

That's it.

For simplicity, only the essential parts ofSetup.exe are described above. Things that are specific to AeroZoom (e.g. removing scheduled tasks that AeroZoom may have created if specified by user) are not mentioned. For the rest, please refer to the comments in thesource code fileSetup.ahk and the comments inside.

It would be my pleasure if you find this article informative. Welcome tocomment if you have any question or suggestion. Have a nice day!

About

How-to: Building an unattended installer and uninstaller using AutoHotkey and pushing to Chocolatey repository

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

    Packages

    No packages published

    [8]ページ先頭

    ©2009-2025 Movatter.jp