iTranslated by AI
Executing Batch Files with Specific User Permissions in Jenkins on Windows
Since Windows 11 Home does not include the Group Policy Editor, you cannot select Run service as local or domain user as the Logon Type during Jenkins installation. Consequently, you must install it with the Run service as LocalSystem setting.
However, when the service is run as LocalSystem, any attempt to run batch files or other commands from Jenkins will be executed as the system user. As a result, paths and authentication credentials configured for the logged-in user cannot be used, causing the process to fail.
In such cases, the runas command is generally available when operating from the command prompt, but it cannot be used via Jenkins because password entry is not possible during authentication. Therefore, we will solve this using Task Scheduler.
Windows Task Scheduler allows you to specify the user account used for execution when a task is run. We will try executing batch files through a task using the schtasks command from Jenkins.
To change the service execution user and persist the settings, please refer to this article.
Execution Environment
- Windows 11 Home
- Jenkins 2.414.3
Steps
- Press
Win+Rand runtaskschd.msc.
- Once Task Scheduler starts, right-click
Task Scheduler Libraryin the left pane and selectCreate Task....
- Select the
Generaltab and enter an arbitrary name in theNamefield.
- Select the
Actionstab and click theNew...button.
- For
Action, selectStart a program, and inProgram/script, specify the.batfile you want to execute.
- Configure other settings as desired and click
OKto register the task. - Create a new job in Jenkins and select
Execute Windows batch commandinBuild Steps. - Enter
schtasks /run /tn task_name(wheretask_nameis your arbitrary task name) into the command field.
With the steps above, you can execute batch files using the privileges of the user configured in Task Scheduler.
Discussion