In this article, I'll discuss how you can use a batch script to keep compliant with software licensing.
•The script, which can be downloaded here or at the end of this article.
•A terminal server that you have administrator access to.
For this tutorial, I'm going to step through the script to explain what happens.
Section 1: Variable declarations and headers:
Rem How many liceses do we have available?
Rem What is the program's executable called?
Rem Define and set variables...
Basically, all I'm doing here is declaring some variables, setting one of them equal to 0, and forcing the script to localize all these variables within this script.
Localizing the variables isn't absolutely necessary, but this helps to prevent variables from mixing up between your scripts, especially if you merge this script with other scripts.
To customize this script, you should set LicensedUses equal to the number of licenses you have for your software. Also, you should set ProgramExecutable to the name of the executable that you are trying to track the licenses for.
Section 2: Query the server and see who is using the executable:
Rem Let's iterate through our servers and see if %executable% is running...
For /f "delims=* " %%i in ('qappsrv.exe') do call :Next %%i
The For command runs the QAppSrv.exe command, which lists all the terminal servers on your network. The output of the QAppSrv command is then handed off to the Next function.
Section 3: Obtain a count to see how many times the executable is being used:
Rem Checking the servers to see where the user is and update the count...
For /f %%j in ('qprocess /server:%1 * ^| find /c /i "%ProgramExecutable%"') do Set /A CurrentUses = %CurrentUses%+%%j
While this portion of the script is out of order, I've put it here just for this tutorial. I would encourage you to download the script to see the correct order of this script.
This Next loop goes through all your terminal servers (identified by QAppSrv.exe) and queries them all to see if ProgramExecutable is running. The script updates the count of running licensed processes by updating the CurrentUses value
Section 4: Check the license use:
Rem CurrentUses will be equal the number of uses right now.
Rem If CurrentUses > LicensedUses, then we are out of licenses. Warn the user and terminate the script.
If %CurrentUses% LSS %LicensedUses% Goto StartProgram
Rem Bummer....not enough licenses. Warn the user and quit.
Msg /W %WinStationName% Error! There are no more licenses available to start the desired program.
After all instances of ProgramExecutable have been counted, the script compares that count to the number of licenses you have. If the count exceeds the number of licenses you have, the script returns an error to the user and quits.
Section 5: Run the program and cleanup before we exit:
Rem Start %executable%...
If the script has made it this far, then the program to be run has enough licenses. The script then starts the program and ends localization of variables.
As I said before, localized variables aren't necessary, but it helps to keep things encapsulated.
As a side note...this script will only track licenses for programs that are called through this script. If a user calls the application directly and bypasses this script, then no license compliance check will be made.
Well, that's about it. You've just finished a walkthrough tutorial for this script.
And...no tutorial would be complete without a download, so here is the script in its entirety (compressed):