Scripting
Leave a comment

Running script as a Windows Service

I have a small WMI script which provides information about Removable storage as and when they are plugged in or removed. I always thought that it would be good if I can run this script as a service in the background so I don’t have to start the script myself every time I reboot or keep it in my startup group or dedicate a command prompt window to run the script.

I came across a small tool, which used to be part of Windows Resource Kit tools, which is a part of Windows XP now. It is called SC.exe. This tool is a multi purpose tool which can be used to manage services under Windows. This has a very nice feature of creating user defined services.

SC Command

In the above command, binpath is used to specify the path to your script. Since VBS cannot run on its own, we require an engine to initiate the script. You can use either cscript or wscript. Type= interact option would make this service an interactive one. This would mean that any messages sent by script are directly displayed on the user’s desktop.

Update:
This works only till Windows XP. On Vista and Longhorn Server, Microsoft moved all services to Session 0. This just means that services run in isolation and interactive services wont function anymore. There must be a programatic way to message between sessions but I haven’t explored anything on that front yet.