Installing a Jenkins Agent as a Windows Service

Install Java 11 from jdk

Create service user as Administrator:

Install-Module -Name 'Carbon' -AllowClobber
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Import-Module 'Carbon'
net user jenkins <password> /ADD
Grant-CPrivilege -Identity "jenkins" -Privilege SeServiceLogonRight

On Jenkins master, create agent node, selecting "Launch agent by connecting it to the controller"
Usage: Only build jobs with label...
Workdir: C:\jenkins

On Agent server
Download winsw-2.9.0-net461.exe
Put in C:\users\jenkins\agent and rename to jenkins-agent.exe

Create jenkins-agent.xml and put in the following, changing secret and jnlp and agent location.  Agent is downloaded from: http://<jenkins master>/jnlpJars/agent.jar:

  <name>Jenkins agent</name>
  <description>This service runs an agent for Jenkins automation server.</description>
  <arguments>-Xrs -jar &quot;%BASE%\agent.jar&quot; -jnlpUrl <JNLP LOCATION> -secret <SECRET> -workDir "C:\jenkins"</arguments>
  <onfailure action="restart">
    <download from="<AGENT JAR LOCATION>" to="%BASE%\agent.jar">
        <extension className="winsw.Plugins.RunawayProcessKiller.RunawayProcessKillerExtension" enabled="true" id="killOnStartup">

Create jenkins-agent.exe.config and put in the following:

        <supportedruntime version="v4.0"/>

Go to powershell and execute the following as admin: jenkins-agent.exe install. This will install the service.  Go to the Services window (services.msc) and change the "Log On" info to use the jenkins service account.

This should start the agent and create the C:\jenkins directory (might have to create the directory and grant permissions to jenkins user)

Start the service, verify connectivity from jenkins master.  Test job using powershell plugin, and just try "whoami /all" to get user details