Executing WSL commands from a Windows service

I’m working on setting up a build pipeline (for my job) to build a custom Linux OS image for our hardware. We use Bamboo as the frontend. I have a Bamboo project set up for this, which connects to a Bamboo remote agent application running on a Windows VM.

On this Windows VM, I have set up Debian inside of WSL and set up the OS image build system inside of it. The Bamboo remote agent executes a batch file called build.bat which then executes some WSL commands.

The build.bat file contains the following:

@ECHO OFF

REM Change working directory to where svn files are checked out to
echo Project directory: %1
cd %1

echo Build Linux OS Image

    REM Run the Linux build scripts
    
    echo Converting sync_build.sh to Unix-style line endings
    wsl --user user -e /bin/bash -xc "sudo dos2unix /home/user/img_build/sync_build.sh"
    
    echo Syncing WSL image build directory with Windows filesystem
    wsl --user user -e /bin/bash -xc "sudo /home/user/img_build/sync_build.sh"
    
    echo Executing Linux OS image build script
    wsl --user user -e /bin/bash -xc "sudo /home/user/img_build/build.sh"

    IF %ERRORLEVEL% == 0 (goto BuildEnd)

    echo Error building Linux OS image
    goto BuildError

:BuildError
echo Exiting due to error
EXIT /B 1

:BuildEnd

When I run the build from the Bamboo frontend, it sends a command to the Bamboo remote agent which then checks out the latest code from SVN and then runs the build.bat file.

If I run the build.bat myself, the OS image build runs fine and finishes successfully. However, when run by the Bamboo remote agent service, I get one of two errors when it attempts to run the wsl commands:

Access is denied – When the service is running as “Local System Account”
The file cannot be accessed by the system – When the service is running as the same account I used when running the batch file myself.

By this, I mean I went into the service properties dialog, under the Log On tab and chose “This Account” (and chose the same account I was logged into).

I’m much more experienced with Linux than I am with Windows, but this appears to be some sort of permissions issue. The VM is running Windows 10 if that matters.

How can I get the service to execute these WSL commands?