I continue the theme of creating non-trivial bat-files for trivial tasks, started
here .
Surely, many faced with the task, when for any purpose in Windows it is necessary to create a task that runs on a schedule (scheduled task).
For these purposes there is a simple graphical interface. However, what if the task should be created automatically?
Let's try to solve this problem using a primitive bat-script that will be executed in almost any version of Windows.
For convenience, we will create a local technological user on the machine, under which our task will run on a schedule. This is convenient because you can set permissions for a user that are necessary only to perform certain actions.
:: The name of the local user under which we will work
set user_name = test_user
:: Password for local user
set user_passw = test_passw
')
And as you know the user must be in a group with certain rights. Here a certain complexity arises, since if the script clearly specifies the name of the group, then problems may arise on a machine with a different location, such as Chinese. And what will be called in the Chinese group "Users" will not be very easy to learn. Fortunately, in Windows, groups are tied to the so-called
Group SID . Knowing, for example, the Group SID of the Administrators group, we can use it in the script. For example, S-1-5-32-545 are local users, and S-1-5-32-544 are administrators.
Now you need to determine the name for the specified Group SID used in this localization. This is where WMIC (WMI command-line) comes to the rescue.
:: S- 1 - 5 - 32 - 545 - local users
Set GroupSID = S- 1 - 5 - 32 - 545
Set GroupName =
For / F "UseBackQ Tokens = 1 * Delims ==" %% I In ( ` WMIC Group Where " SID = '% GroupSID%' " Get Name / Value ^ | Find " = " ` ) Do Set GroupName = %% J
Set GroupName = % GroupName: ~ 0 , - 1 %
Need to know another nuance. When creating a user, depending on the system settings, a password expiration time is set. And if the password needs to be changed, the task will not be executed according to the schedule. To do this, we need to create a user whose password never expires. You cannot set this in the standard net user command (expires: never - specifies that the user cannot change the password), so again we will resort to using
WMIC :
:: Create user
net user % user_name%% user_passw % / add / comment: "User for works with application" / expires: never / fullname: % user_name % / passwordchg: no
:: Set so that the password never expires
:: Or so - wmic path Win32_UserAccount where Name = '% user_name%' set PasswordExpires = false
wmic USERACCOUNT where Name = '% user_name%' set PasswordExpires = false
:: Adding a local user to the specified local group
net localgroup % GroupName%% user_name % / ADD
Please note that if you delete a user with the
net user test_user / DELETE command, then you will need to manually delete its directory along the path% USERS% \ test_user \ or provide for its deletion in the script.
Well, then create the task itself, which is performed according to a schedule:
:: The name of the scheduled task under which the application will run
set task_name = Test_task_bat
:: Application Path
set my_app_path = "d: test.bat"
:: Interval of the application in the temporary task
:: Valid schedule types: MINUTE, HOURLY, DAILY, WEEKLY, MONTHLY, ONCE, ONSTART, ONLOGON, ONIDLE.
:: EVERY MONTHLY, EVERY MONTHLY, EVERYDAY, WEEKLY, MONTHLY ON STARTING ENTRY INTO THE SYSTEM ON SIMPLE
set schtasks_time = MINUTE
:: Initial start time of the application in the temporary task
set schtasks_start = 08: 00: 00
:: Creating a scheduled task
schtasks / create / tn "% task_name%" / tr % my_app_path % / sc % schtasks_time % / st % schtasks_start % / ru % user_name % / rp % user_passw %
That's all. I hope that my small manual will be useful and you will save your time when performing this task.
PS
I foresee such questions and comments: there are more convenient tools, why exactly bat?
Just for fun!