jalanga
jalanga

Reputation: 1576

Electron, electron-builder, nsis, remove SchTasks at uninstall

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
},

Upvotes: 2

Views: 2854

Answers (4)

jalanga
jalanga

Reputation: 1576

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}"
},

Upvotes: 1

Rustam
Rustam

Reputation: 1923

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

Upvotes: 0

idleberg
idleberg

Reputation: 12882

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
}

Upvotes: 0

Anders
Anders

Reputation: 101764

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+.

Upvotes: 1

Related Questions