0

I am using nsis to create installer for windows and I am using NSSM to run the application.

everything is fine when I install my server , but the problem my server needs to run as admin in order to use some functionalities. I solved my problem by manually going to "services" then Logon tab to enter my username and password. enter image description here

However , I do not want to do that, I want the installer to ask the user to enter his admin credentials right after installing or before installing.

thanks in advance for your time and efforts

castro
  • 37
  • 7

1 Answers1

0

According to their documentation, NSSM already knows how to set service credentials so all you need is the custom page to tie it all together:

!include LogicLib.nsh
!include nsDialogs.nsh

!define MYSERVICE "FooBarBaz"
Var SvcUser
Var SvcPass

Page Directory
Page Custom MyServiceCredCreate MyServiceCredLeave ": Service credentials "
Page InstFiles

Function MyServiceCredCreate
nsDialogs::Create 1018
Pop $0
${IfThen} $0 == error ${|} Abort ${|}
${NSD_CreateLabel} 0 0 100% 12u "Username:"
Pop $0
${NSD_CreateText} 0 13u 100% 12u "$SvcUser"
Pop $1
${NSD_CreateLabel} 0 26u 100% 12u "Password:"
Pop $0
${NSD_CreatePassword} 0 39u 100% 12u "$SvcPass"
Pop $2
nsDialogs::Show
FunctionEnd

Function MyServiceCredLeave
${NSD_GetText} $1 $SvcUser
${NSD_GetText} $2 $SvcPass
${If} $SvcUser == ""
  MessageBox MB_ICONSTOP "Must provide a username!"
  Abort
${EndIf}
FunctionEnd

Section
SetOutPath $InstDir
File "myservicething.exe"
File "nssm.exe"
nsExec::Exec '"$InstDir\nssm.exe" install "${MYSERVICE}" "$InstDir\myservicething.exe"'
Pop $0
nsExec::Exec '"$InstDir\nssm.exe" set "${MYSERVICE}" ObjectName $SvcUser "$SvcPass"'
Pop $0
SectionEnd
Anders
  • 97,548
  • 12
  • 110
  • 164
  • @andres thank you for reply , so I do not want to create credentials , I want to use admin credentials and run the server as administer. I added your code but it did not work , the custom page was there and if i enter any username or password , it will accept it without any problem. Also, how do you run it ? i used nsExec::Exec '"$InstDir\nssm.exe" start "${MYSERVICE}"' – castro Dec 06 '21 at 18:30
  • I don't know what you mean by admin credentials, the user will have to enter a password. Yes, that start command looks alright. – Anders Dec 06 '21 at 19:44
  • I have added an image to my question , my application works when I enter admin's username and admin's password then confirm that password. so I want the same thing but I want get that when I am installing the application. @andres – castro Dec 06 '21 at 19:52
  • Just enter your admin credentials in my example? – Anders Dec 06 '21 at 19:58
  • this is what I did and it did not work , also when I entered wrong username and password , it accepted it and it did not complain – castro Dec 06 '21 at 20:04
  • It does not actually check if they are correct. Anyway, you should verify that the nssm set ObjectName command actually works outside a installer. It is easier if we know where the problem is. – Anders Dec 06 '21 at 20:10
  • thanks a lot @andres , it working now . I had to enter the hostname/username as username. now I will try to get the hostname automatically so the user enters only username – castro Dec 07 '21 at 14:50