Installing a Jenkins Agent as a Windows Service

Install Java 11 from azul.com: jdk 11.0.14.1

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:

<service>
  <id>JenkinsAgent</id>
  <name>Jenkins agent</name>
  <description>This service runs an agent for Jenkins automation server.</description>
  <executable>%JAVA_HOME%\bin\java.exe</executable>
  <arguments>-Xrs -jar &quot;%BASE%\agent.jar&quot; -jnlpUrl <JNLP LOCATION> -secret <SECRET> -workDir "C:\jenkins"</arguments>
  <logmode>rotate</logmode>
  <onfailure action="restart">
    <download from="<AGENT JAR LOCATION>" to="%BASE%\agent.jar">
      <extensions>
        <extension className="winsw.Plugins.RunawayProcessKiller.RunawayProcessKillerExtension" enabled="true" id="killOnStartup">
          <pidfile>%BASE%\jenkins_agent.pid</pidfile>
          <stopTimeout>5000</stopTimeout>
          <stopParentFirst>false</stopParentFirst>
        </extension>
      </extensions>
    </download>
  </onfailure>
</service>

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

<configuration>
    <startup>
        <supportedruntime version="v4.0"/>
    </startup>
</configuration>

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