Monday, May 28, 2018

windows services - installation / deinstallation toggle script

Tried to find a script that installs a windows service if it is not installed and is capable to deinstall it if needed. Finally, I created myself the following:

sc query SERVICENAMEif %errorlevel% equ 0 (                echo uninstall                goto uninstall) else (                echo install                goto install)

Check if it is installed: if so uninstall, if not install. 

in :install call:

  • sc create SERVICENAME binpath=%cd%\Service.exe displayname="the service" start=demand 
    • if you need to set credentials for the startup then set: obj and password
    • %cd% is the current directory
  • sc start SERVICENAME


in :uninstall call:
  • sc stop SERVICENAME
  • sc delete SERVICENAME
    • delete can already be called even if the service is still in "stopping" state so in a script it works the right way even with some timing issues

No comments: