iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💻

Executing Batch Files with Specific User Permissions in Jenkins on Windows

に公開
2023/11/16

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.
https://zenn.dev/sion_pn/articles/0ceffb3297ffa2

Execution Environment

  • Windows 11 Home
  • Jenkins 2.414.3

Steps

  • Press Win+R and run taskschd.msc.
  • Once Task Scheduler starts, right-click Task Scheduler Library in the left pane and select Create Task....
  • Select the General tab and enter an arbitrary name in the Name field.
  • Select the Actions tab and click the New... button.
  • For Action, select Start a program, and in Program/script, specify the .bat file you want to execute.
  • Configure other settings as desired and click OK to register the task.
  • Create a new job in Jenkins and select Execute Windows batch command in Build Steps.
  • Enter schtasks /run /tn task_name (where task_name is 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