2

I made an app that is running with Admin Privileges. To run the app at windows startup I made an SchTasks, but at uninstall I want to remove it. The closest I could get is:

;script used to remove the auto launch scheduled task

!macro customUnInstall
  ExpandEnvStrings $0 %COMSPEC%
  ExecWait `"$0" /c "SchTasks /Delete /TN task_name /F & pause"`
!macroend

But it returns ERROR: Access is denied.. This is because the uninstall doesn't have admin priv. What should I do, should I try to make the uninstall to be executed with admin priv? Or there is another way to remove the task?

Another option in my mind is to make the task to delete it self if the executable is not in path.

The electron package.json I am using:

"win": {
  "target": [
    "nsis"
  ],
  "requestedExecutionLevel": "requireAdministrator"
},
"nsis": {
  "include": "installer/windows/uninstall.nsh",
  "allowElevation": true,
  "deleteAppDataOnUninstall": true
},
jalanga
  • 1,456
  • 2
  • 17
  • 44

4 Answers4

1

I don't know anything about Electron-builder but I do know that if the installer script has RequestExecutionLevel Admin then the uninstaller will also request elevation on Vista+.

Anders
  • 97,548
  • 12
  • 110
  • 164
  • On windows 10 it hasn't – jalanga Jun 27 '18 at 14:58
  • It does not matter which machine you generate the installer on, it is a manifest attribute stored in the installer&uninstaller .exe, you just need to figure out how to set it with electron-builder. – Anders Jun 27 '18 at 15:08
1

Here is my solution, in nsh file.

!macro customHeader
   RequestExecutionLevel admin
!macroend

!macro customUnInstall
${ifNot} ${isUpdated}
    ; remove the scheduled task
    ExpandEnvStrings $0 %COMSPEC%
    ExecWait `"$0" /c "SchTasks /Delete /TN name /F"`

    ; delete registry for uninstaller - run as admin
    SetRegView 64
      DeleteRegValue HKCU "SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" "$LOCALAPPDATA\Programs\name\Uninstall name.exe"
    SetRegView 32
      DeleteRegValue HKCU "SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" "$LOCALAPPDATA\Programs\name\Uninstall name.exe"
  ${endIf}
!macroend

package.json

"win": {
  "target": [
    "nsis"
  ],
  "requestedExecutionLevel": "requireAdministrator"
},
"nsis": {
  "include": "installer/windows/installer.nsh",
  "allowElevation": true,
  "deleteAppDataOnUninstall": true,
  "artifactName": "${productName}.${ext}"
},
jalanga
  • 1,456
  • 2
  • 17
  • 44
  • 1
    No, I am getting error `Error: Exit code: EACCES. spawn ...setup-0.9.2.exe EACCES` and reason in customHeader macro. Every library is up to date ( both electron-builder and electron) – Rustam Nov 07 '19 at 10:51
  • I cannot use "requestedExecutionLevel": "requireAdministrator". Only request when it really needed – Rustam Nov 07 '19 at 10:57
  • Concerning the `Error: Exit code: EACCES` while packaging, it seems that running your `npm electron-builder ...` command in an admin console solves the issue. – Wasabi Jun 15 '21 at 07:40
  • Note that `"requestedExecutionLevel": "requireAdministrator"` does not affect the installer privilege level but only the **installed application** required privileges. – Wasabi Jun 15 '21 at 09:28
0

As mentioned in the documentation, you need to add the following to your electron-builder.json (or the build section of your package.json) to elevate your installer:

"nsis": {
    "allowElevation": true
}
idleberg
  • 12,634
  • 7
  • 43
  • 70
0

I found another way, but it will break your "one click" installation:

"build": {
    "nsis": {
      "include": "./build/installer.nsh",
      "oneClick": false,
      "perMachine": true,
      "warningsAsErrors": false
    }
  },

Key thing: oneClick false + perMachine true

Rustam
  • 1,875
  • 2
  • 16
  • 33