If you need your application or script to be started, whenever Windows is booted, you need to install it as a service (so it will even be started when you didn’t login yet). Microsoft offers a little tool to easily create your own service and make it run some batch file / application as a part of their Windows Server Resource Kit Tools. From this package, you only need srvany.exe and instsrv.exe to install your custom service, but you still need to perform all this manual configuration before your service will be running.
I created the following batch script to easily install or uninstall your custom application as a service. All required information will be requested interactively to the user step by step. No further manual configuration will be needed anymore. Errors will be shown with colors and attention points will be flashed.
@echo offecho.echo.echo Use this script to install / uninstall your custom applications or scripts as a service and make sure they will be started whenever Windows is booted.SET DFAULT_BACKGROUND_COLOR=0SET DEFAULT_TEXT_COLOR=7SET ERROR_BACKGROUND_COLOR=4SET ERROR_TEXT_COLOR=FSET ATTENTION_BACKGROUND_COLOR=0SET ATTENTION_TEXT_COLOR=Frem 0 = Blackrem 1 = Bluerem 2 = Greenrem 3 = Aquarem 4 = Redrem 5 = Purplerem 6 = Yellowrem 7 = Whiterem 8 = Grayrem 9 = Light Bluerem A = Light Greenrem B = Light Aquarem C = Light Redrem D = Light Purplerem E = Light Yellowrem F = Bright White:MAIN_MENUCOLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%SET RETURN_DRAW_ATTENTION_POINT=MAIN_MENU_INPUTGOTO DRAW_ATTENTION:MAIN_MENU_INPUTecho.echo.SET INPUT=echo To Install a new custom service, press I.echo To Uninstall a previously created custom service, press U.echo To open the Windows Services application, press S.set /p INPUT=Press E to Exit:if /i "%INPUT%" == "i" goto START_INSTALLif /i "%INPUT%" == "u" goto START_UNINSTALLif /i "%INPUT%" == "s" goto START_SERVICESif /i "%INPUT%" == "e" goto EXITGOTO MAIN_MENU:END_MAIN_MENU_INPUTecho.:END_MAIN_MENU:START_INSTALLrem INTERACTIVE MODE is used, lines below kept for reference:HARD_CODED_PATHS_AND_NAMESrem APP_NAME=REPLACE_THIS_WITH_YOUR_CUSTOM_APP_NAMErem APP_PATH=FULL_PATH_TO_CUSTOM_APPrem one can use %CD% in the path, which will be replace by the current directoryrem uncomment following 2 lines if you want to use the folder name (where this bat file is run) as service name APP_NAMErem SET APP_NAME=rem for %%* in (.) do set APP_NAME=%%~n*:INTERACTIVE_INPUT_OF_PATHS_AND_NAMES:INTERACTIVE_INPUT_APP_NAMEecho.SET APP_NAME=set /p APP_NAME=Provide the name of your custom application (will be used as the name of the service):echo.:END_INTERACTIVE_INPUT_APP_NAME:INTERACTIVE_INPUT_OF_APP_PATHecho.SET APP_PATH=set /p APP_PATH=Provide the full path of your custom application to run as a service (don't use quotes):echo.:END_INTERACTIVE_INPUT_OF_APP_PATH:CHECK_CUSTOM_APP_PATHIF EXIST "%APP_PATH%" GOTO END_CHECK_CUSTOM_APP_PATHecho.echo Your custom application %APP_NAME% could not be found at %APP_PATH%!echo Please try again!SET RETURN_DRAW_ERROR_ATTENTION_POINT=INTERACTIVE_INPUT_OF_APP_PATHGOTO DRAW_ERROR_ATTENTION:END_CHECK_CUSTOM_APP_PATHCOLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%:INTERACTIVE_INPUT_APP_FOLDERecho.SET APP_FOLDER=set /p APP_FOLDER=Provide the folder to be used as working directory for your custom application (press ENTER if not required) (don't use quotes):echo.:END_INTERACTIVE_INPUT_APP_PARAM:INTERACTIVE_INPUT_APP_PARAMecho.SET APP_PARAM=set /p APP_PARAM=Provide the parameters of your custom application (press ENTER if no parameters are required) (don't use quotes):echo.:END_INTERACTIVE_INPUT_APP_PARAM:INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATHecho.SET INST_SERVICE_APP_PATH=set /p INST_SERVICE_APP_PATH=Provide the full path to instsrv.exe (don't use quotes). LEAVE EMPTY to use default path (%CD%\instsrv.exe)::END_INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH:CHECK_INST_SERVICE_APP_PATH_INPUTif "%INST_SERVICE_APP_PATH%"=="" goto DEFAULT_INST_SERVICE_APP_PATH:END_CHECK_INST_SERVICE_APP_PATH_INPUT:CHECK_INST_SERVICE_APP_PATHIF EXIST "%INST_SERVICE_APP_PATH%" GOTO END_CHECK_INST_SERVICE_APP_PATHecho.echo instsrv.exe could not be found at %INST_SERVICE_APP_PATH%!echo Please try again!SET RETURN_DRAW_ERROR_ATTENTION_POINT=INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATHGOTO DRAW_ERROR_ATTENTION:END_CHECK_INST_SERVICE_APP_PATHCOLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%:INTERACTIVE_INPUT_OF_SERVICE_APP_PATHecho.SET SERVICE_APP_PATH=set /p SERVICE_APP_PATH=Provide the full path to srvany.exe (don't use quotes). LEAVE EMPTY to use default path (%CD%\srvany.exe)::END_INTERACTIVE_INPUT_OF_SERVICE_APP_PATH:CHECK_SERVICE_APP_PATH_INPUTif "%SERVICE_APP_PATH%"=="" goto DEFAULT_SERVICE_APP_PATH:END_CHECK_SERVICE_APP_PATH_INPUT:CHECK_SERVICE_APP_PATHIF EXIST "%SERVICE_APP_PATH%" GOTO END_CHECK_SERVICE_APP_PATHecho.echo srvany.exe could not be found at %SERVICE_APP_PATH%!echo Please try again!SET RETURN_DRAW_ERROR_ATTENTION_POINT=INTERACTIVE_INPUT_OF_SERVICE_APP_PATHGOTO DRAW_ERROR_ATTENTION:END_CHECK_SERVICE_APP_PATHCOLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%:END_INTERACTIVE_INPUT_OF_PATHS_AND_NAMES:SHOW_CONFIGecho.echo Ready to install your custom application, using the following configuration:echo.echo Custom application name: %APP_NAME%echo Custom application full path: %APP_PATH%echo Custom application parameters: %APP_PARAM%echo Custom application working directory: %APP_FOLDER%echo srvany.exe full path: %SERVICE_APP_PATH%echo instsrv.exe full path: %INST_SERVICE_APP_PATH%echo.SET RETURN_DRAW_ATTENTION_POINT=SHOW_CONFIG_WAITGOTO DRAW_ATTENTION:SHOW_CONFIG_WAITCOLOR %ATTENTION_BACKGROUND_COLOR%%ATTENTION_TEXT_COLOR%set INPUT=set /p INPUT=Press ENTER to continue installation, all other will return to main menu:if /i not "%INPUT%" == "" GOTO MAIN_MENUCOLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%:END_SHOW_CONFIG_WAIT:END_SHOW_CONFIG:INSTALL_SERVICE"%INST_SERVICE_APP_PATH%" "%APP_NAME%" "%SERVICE_APP_PATH%"echo.echo Service installed : %APP_NAME%echo.:END_INSTALL_SERVICE:UPDATE_REGISTRYREG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%APP_NAME%\Parameters" /v "Application" /t REG_SZ /d "%APP_PATH%" /fIF NOT "%APP_FOLDER%"=="" REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%APP_NAME%\Parameters" /v "AppDirectory" /t REG_SZ /d "%APP_FOLDER%" /fIF NOT "%APP_PARAM%"=="" REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%APP_NAME%\Parameters" /v "AppParameters" /t REG_SZ /d "%APP_PARAM%" /fecho.echo Registry updated to run %APP_PATH% with service %APP_NAME%echo.:END_UPDATE_REGISTRY:START_SERVICESET RETURN_DRAW_ATTENTION_POINT=START_SERVICE_INPUT_REQUESTGOTO DRAW_ATTENTION:START_SERVICE_INPUT_REQUESTecho The service is now installed successfully (if no errors are logged above!)set INPUT=set /p INPUT=To start the service now, press Y. All others will return to main menu:if /i not "%INPUT%" == "y" goto END_START_SERVICE:END_START_SERVICE_INPUT_REQUESTNET START "%APP_NAME%"echo.echo Service %APP_NAME% started:END_START_SERVICEGOTO END_START_INSTALL:DEFAULT_INST_SERVICE_APP_PATHecho Using default path for instsrv.exe %INST_SERVICE_APP_PATH%SET INST_SERVICE_APP_PATH=%CD%\instsrv.exeGOTO END_CHECK_INST_SERVICE_APP_PATH_INPUT:END_DEFAULT_INST_SERVICE_APP_PATH:DEFAULT_SERVICE_APP_PATHecho Using default path for srvany.exe %SERVICE_APP_PATH%SET SERVICE_APP_PATH=%CD%\srvany.exeGOTO END_CHECK_SERVICE_APP_PATH_INPUT:END_DEFAULT_SERVICE_APP_PATH:END_START_INSTALLGOTO MAIN_MENU:START_SERVICESstart "Services" services.msc:END_START_SERVICESGOTO MAIN_MENU:START_UNINSTALL:INTERACTIVE_INPUT_APP_NAME_UNINSTALLecho.SET APP_NAME=set /p APP_NAME=Provide the name of your custom application (the name of the service to uninstall):echo.:END_INTERACTIVE_INPUT_APP_NAME_UNINSTALL:INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH_UNINSTALLecho.SET INST_SERVICE_APP_PATH=set /p INST_SERVICE_APP_PATH=Provide the full path to instsrv.exe (don't use quotes). LEAVE EMPTY to use default path (%CD%\instsrv.exe)::END_INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH_UNINSTALL:CHECK_INST_SERVICE_APP_PATH_INPUT_UNINSTALLif "%INST_SERVICE_APP_PATH%"=="" goto DEFAULT_INST_SERVICE_APP_PATH_UNINSTALL:END_CHECK_INST_SERVICE_APP_PATH_INPUT_UNINSTALL:CHECK_INST_SERVICE_APP_PATH_UNINSTALLIF EXIST "%INST_SERVICE_APP_PATH%" GOTO END_CHECK_INST_SERVICE_APP_PATH_UNINSTALLecho.echo instsrv.exe could not be found at %INST_SERVICE_APP_PATH%!echo Please try again!SET RETURN_DRAW_ERROR_ATTENTION_POINT=INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH_UNINSTALLGOTO DRAW_ERROR_ATTENTION:END_CHECK_INST_SERVICE_APP_PATH_UNINSTALLCOLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%:STOP_SERVICENET STOP %APP_NAME%echo.echo Service %APP_NAME% stoppedecho.:END_STOP_SERVICE:REMOVE_SERVICE"%INST_SERVICE_APP_PATH%" %APP_NAME% REMOVEecho.echo Service %APP_NAME% removedecho.:END_REMOVE_SERVICE:UPDATE_REGISTRY_UNINSTALLREG DELETE HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%APP_NAME%\ /va /fecho.echo Registry cleaned up for service %APP_NAME%echo.:END_UPDATE_REGISTRY_UNINSTALLGOTO END_START_UNINSTALL:DEFAULT_INST_SERVICE_APP_PATH_UNINSTALLecho Using default path for instsrv.exe %INST_SERVICE_APP_PATH%SET INST_SERVICE_APP_PATH=%CD%\instsrv.exeGOTO END_CHECK_INST_SERVICE_APP_PATH_INPUT_UNINSTALL:END_DEFAULT_INST_SERVICE_APP_PATH_UNINSTALL:END_START_UNINSTALLGOTO MAIN_MENU:EXIT_WITH_PAUSEecho The end. Comments and updates: http://myTselection.blogspot.com, Copyright MightyMouse. Copyright srvany and instsrv Microsoft.pauseGOTO EXIT:END_EXIT_WITH_PAUSE:EXITexit:END_EXIT:DRAW_ATTENTIONCOLOR %ATTENTION_BACKGROUND_COLOR%%ATTENTION_TEXT_COLOR%@PING 1.1.1.1 -n 1 -w 100 >NULCOLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%@PING 1.1.1.1 -n 1 -w 100 >NULCOLOR %ATTENTION_BACKGROUND_COLOR%%ATTENTION_TEXT_COLOR%@PING 1.1.1.1 -n 1 -w 100 >NULCOLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%@PING 1.1.1.1 -n 1 -w 100 >NULCOLOR %ATTENTION_BACKGROUND_COLOR%%ATTENTION_TEXT_COLOR%@PING 1.1.1.1 -n 1 -w 100 >NULCOLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%:END_DRAW_ATTENTIONGOTO %RETURN_DRAW_ATTENTION_POINT%:DRAW_ERROR_ATTENTIONCOLOR %ERROR_BACKGROUND_COLOR%%ERROR_TEXT_COLOR%@PING 1.1.1.1 -n 1 -w 100 >NULCOLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%@PING 1.1.1.1 -n 1 -w 100 >NULCOLOR %ERROR_BACKGROUND_COLOR%%ERROR_TEXT_COLOR%@PING 1.1.1.1 -n 1 -w 100 >NULCOLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%@PING 1.1.1.1 -n 1 -w 100 >NULCOLOR %ERROR_BACKGROUND_COLOR%%ERROR_TEXT_COLOR%@PING 1.1.1.1 -n 1 -w 100 >NULCOLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%@PING 1.1.1.1 -n 1 -w 100 >NULCOLOR %ERROR_BACKGROUND_COLOR%%ERROR_TEXT_COLOR%:END_ERROR_DRAW_ATTENTIONGOTO %RETURN_DRAW_ERROR_ATTENTION_POINT%@echo on
A full package to easily install your own custom application as a service can be downloaded here, sources (zip).
Update (26/04/09): Added possibility to specify application working directory and command line parameters. (AppDirectory and AppParameter registry keys.)
Update (09/10/2011): New links
No comments:
Post a Comment