windowsjenkinswindows-10onedrive

Bat file runs locally from cmd, but loops when ran using Jenkins


I created the following .bat file in order to shutdown onedrive service:

whoami
"C:\Program Files\Microsoft OneDrive\OneDrive.exe" /shutdown

If I double-click on the .bat icon file, this works. Also, this works when I run a command line shell and type "C:\Users\john\Desktop\onedrive-shutdown.bat".

Now, I am running a Jenkins service on the same windows server which is connected as john (logon properties) and I created a project which consists of the following build step:

enter image description here

Unfortunately, the /shutdown process hangs up/loops since I am getting the following output:

whoami
john
"C:\Program Files\Microsoft OneDrive\OneDrive.exe" /shutdown

(and ... icon. Also, OneDrive won't be shutdown).

What am I missing?

Note that:


Solution

  • The issue arises because Jenkins runs as a system process (0), while OneDrive requires an interactive user session (1) to function correctly. To resolve this, I created two scheduled tasks to execute the batch files in the correct user contex.

    Here's how I solved it:

    1. Created two batch files so that execute the following commands: "C:\Program Files\Microsoft OneDrive\OneDrive.exe" /shutdown and "C:\Program Files\Microsoft OneDrive\OneDrive.exe" /background.
    2. Created a task for /shutdown command: schtasks /create /tn "OneDriveShutdownTask" /tr "C:\Users\john\Desktop\onedrive-shutdown.bat" /sc onstart /ru john /rl HIGHEST
    3. Created a task for /background command: schtasks /create /tn "OneDriveBackgroundTask" /tr "C:\Users\john\Desktop\onedrive-background.bat" /sc onstart /ru john /rl LIMITED (note LIMITED here, since OneDrive can't be ran in HIGHEST mode).
    4. Launch schtasks /run /tn "OnedriveShutdownTask" or schtasks /run /tn "OnedriveBackgroundTask" either using Windows CMD or using Jenkins.