Using bat files to create Scheduled Tasks

    I continue the topic of creating non-trivial bat-files for trivial tasks, started here .

    Surely many were faced with a task when, for any purpose in Windows, it was necessary to create a 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 run on almost any version of Windows.

    For convenience, we will create a local technological user on the machine, under which our task will work according to the schedule. This is convenient in that for the user you can set the rights 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 the local user
    set user_passw = test_passw


    And as you know, the user must be in a group with certain rights. This is where a certain difficulty arises, because if the script clearly sets the name of the group, then there may be problems on a machine with a different localization, for example, Chinese. And what the “Users” group will be called in Chinese will not be easy to find out. Fortunately, on 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 a 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. Here WMIC (WMI command-line) will come to our aid.

    :: 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%


    You need to know one more nuance. When creating a user, depending on the system settings, the password expiration time is set. And if the password needs to be changed, then the scheduled task will not be executed. To do this, we need to create a user whose password never expires. This can not be set in the standard net user command (expires: never - sets that the user cannot change the password), so again, we will resort to 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
    :: Either 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 given local group
    net localgroup% GroupName%% user_name% / ADD


    Please note that if you delete the user with the net user test_user / DELETE command , you will need to manually delete his directory under the path% USERS% \ test_user \ or provide for its removal in the script.

    Well, then we create the task itself, which is scheduled:

    :: 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.
    :: IMMEDIATELY, MONTHLY, DAILY, MONTHLY, MONTHLY ON STARTING THE ENTRANCE TO THE SYSTEM WHEN
    SIMPLE set schtasks_time = MINUTE
    :: Start time for the application to start in the temporary task
    set schtasks_start = 08: 00: 00
     
    :: "Creating / scheduled task
    t % 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 similar questions and comments: there are more convenient tools, why bat?
    Just for fun!

    Also popular now: