This blog post is going to be interesting mostly for people that got already a lot of experiences with batch scripting.
I am going to talk about quite interesting stuff I call batch services - I love this technology and it saved me and helped me many times in all my previous projects.
Batch service is in fact quite simple - it is just batch, that got actions linked to different folders and it is running periodically.
In my current job, we are using batch services for automatic rebooting of servers and for sending mails, however it was extremely useful for deployment because you were able to do a lot of stuff with very simple scripts.
Consider scenario where you want to allow your technicians to unlock some accounts for example without giving them permissions.
You write script called UnlockAccountEngine.cmd. This script will consist of following lines:
For /f "usebackq delims=. tokens=1,*" %%i IN (`Dir /s /b "\\server\UnlockAccounts$\Account.*"`) Do (
Echo %Date%;%Time%;%UserDomain%;%%j>>"\\Server\UnlockAccounts$\Unlock.log"
Net User %UseDomain%\%%j /Domain /Active:Yes
)
Extremely simple, right? What it means is that it will unlock every account when file Account.UserName is available. For example if I would like to unlock my account, I will just create file Account.Mzugec.
Now we will create client script call UnlockAccount.cmd
This file contains following lines:
@Echo off
Set /p Int.AccountName=Please specify account name which you would like to unlock:
If Exist \\Server\UnlockAccounts$\Account.%Int.AccountName% (
Echo WARNING: Request to unlock this account was already sent
) Else (
Echo.>\\Server\UnlockAccounts$\Account.%Int.AccountName%
)
If Exist \\Server\UnlockAccounts$\Account.%Int.AccountName% (
Echo Request to unlock account %Int.AccountName% is registered, please wait few minutes and try it again
) Else (
Echo WARNING: Request to unlock account %Int.AccountName% was not created, please contact your IT support
)
This script is really simple, you just specify account name and then you will see if request (file) was successfully created or not.
Now we need to perform just last step - create scheduled task called UnlockAccountEngine and schedule it to run every 3 minutes (!).
What will happen? Every 3 minutes script UnlockAccountEngine will run and it will unlock any account that is locked. Extremely simple, but also extremely useful in fact :)
There are many advantages - this script wont hang, you don’t need to delegate any privileges, you can use scripts, web pages, even emails to generate such files (to do something, you just create one file, can it be simplier?)…
There are many different scenarios what you can do with batch engines. For example if you are deployment guy, you can let users "request" software based on their needs, or you can run some program on remote machine (VNC server?) and many many other options.