Wednesday, November 29, 2006

Transform batch file in Windows Service

The goal of the operation are to change a vbs script in a Windows Service.

You need to additionals files availaible on Windows Resource kit - instsrv.exe and srvany.exe. The instsrv.exe can be placed in the windows system32 directory.

In our exemple we try to start a vbscript in windows services. To take this change we execute the following command (the vbscript are started through a batch file):

instsrv vpnreport srvany (vpnreport is the name of the service that can be viewed in the service manager).

At this point, your service has been created. However, if you were to try to start it now, it would fail. You next have to tell SRVANY where your batch file resides, so it can start it when the service starts.

Step 1: Add a "Parameters" key:
Start your registry editor
Find your service under HKLM\SYSTEM\CurrentControlSet\Services\vpnreport
Create a "Parameters" key


Step 2: Add an "Application" value:
Open the "Parameters" key
Create an "Application" string value (REG_SZ) within the "Parameters" key
Edit "Application" and specify the full path of your app executable (including the extension)


Step 3: Add optional values? While not necessary, you may wish to add optional values to the "Parameters" key
AppParameters (String) - Specify any parameters for your application
AppDirectory (String) - Specify the current directory to use for your application


Congratulations! You have just successfully turned your batch file into a Windows service.

There are some other bits of information you should know about SRVANY and your new Windows service application:

- The registry values are not case sensitive
- To un-install your service, use INSTSRV.EXE, eg: INSTSRV vpnreport REMOVE


- There are three ways to start your service :
From the Service manager of the Control Panel
Using sc.exe utility, eg: SC start vpnreport
Using the Net.exe utility, eg: NET START vpnreport

- For stoping the service, you have three ways, same as for starting but by replacing start with stop.

You may install SRVANY.exe several times but with different registry parameters (i.e. running different target applications) - just use a distinct service name for each instance.