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:
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:
john
psexec64 -i 1 -u john -p PASSWORD "C:\Program Files\Microsoft OneDrive\OneDrive.exe" /shutdown
, but this won't work (same results).runas
since user john
user has got a password..bat
file, such as echo "Hello World"
works fine.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:
"C:\Program Files\Microsoft OneDrive\OneDrive.exe" /shutdown
and "C:\Program Files\Microsoft OneDrive\OneDrive.exe" /background
./shutdown
command: schtasks /create /tn "OneDriveShutdownTask" /tr "C:\Users\john\Desktop\onedrive-shutdown.bat" /sc onstart /ru john /rl HIGHEST
/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).schtasks /run /tn "OnedriveShutdownTask"
or schtasks /run /tn "OnedriveBackgroundTask"
either using Windows CMD or using Jenkins.