Skip to content

Instantly share code, notes, and snippets.

@f-steff
Last active August 14, 2024 13:56
Show Gist options
  • Save f-steff/c776aa753e0c88edccee2f44ffe39047 to your computer and use it in GitHub Desktop.
Save f-steff/c776aa753e0c88edccee2f44ffe39047 to your computer and use it in GitHub Desktop.
How to set up OneDrive.exe as a Windows Service, allowing synchronization without a user is logged in.
OneDrive.exe as a Windows Service
=================================
Flemming Steffensen, 2017, 2021
For our automatic build setup, we needed to fetch some files off a Sharepoint Library.
Sharepoint allows (if configured so) Lists and Libraries to be synchronized to a local folder by using the OneDrive application.
However, the OneDrive application is started when a user logs in, and in an automated build setup, this never happens.
The solusion were to disable the normal auto-start feature of OneDrive, and then install it as a service, and making sure the
service start as the computer starts.
==========
Edit February 2021:
While this has been working flawlessly for me, others have had problems when Onedrive.exe were updated. In the comments below, @useyourblinker has made some observations and workarounds to deal with these issues. I highly recommend following his advice!
Additionally, @adrian-alephit pointed out a copy-waste issue with the Onedrive.exe Startup Directory in the text below. This has been fixed.
Huge thanks to both @useyourblinker and @adrian-alephit for their contribution!
==========
To aid this setup, the excellent NSSM tool (https://nssm.cc/) will be used.
1. Log in to the computer using the user you wish to have OneDrive running as. Ensure this user has local admin rights.
2. Install the latest Onedrive from Microsofts download page: https://www.microsoft.com/en-us/microsoft-365/onedrive/download
3. Configure OneDrive so that syncing is working.
If there are security issues, you may have to add trusted sites using IE: IE-Settings→Internet Options→ Security, select Trusted Sites and click on Sites
4. Go to OneDrive settings and ensure to remove the option for OneDrive to start as the user logs in.
Note, in the future, if you need access to the GUI, you will have to ensure the service we create is stopped, before running the OneDrive.exe as normal.
5. Open a command prompt, and change directory to the place you have installed NSSM.
6. Install the Onedrive Service by issuing the following in the command prompt: nssm.exe install "Onedrive Sync NSSM-Service"
7. In the GUI that follows, configure the tabs as follows:
Application TAB
Path: C:\Users\<USER_NAME>\AppData\Local\Microsoft\OneDrive\OneDrive.exe
Startup Directory: C:\Users\<USER_NAME>\AppData\Local\Microsoft\OneDrive
Details Tab
Display Name: OneDrive Sync
Description: Onedrive Sync Service installed using NSSM.
Startup Type: Automatic
Log On
This Account: <USER_NAME>
Password: Correct password of <USER_NAME>
Confirm: Correct password of <USER_NAME>
Dependencies: Leave as default
Process
Priority: Normal
Affinity: Select "All processor"
Console window: Select
Shutdown: Leave as default
Exit actions: Leave as default
I/O
Input (stdin):
Output (stdout): C:\OnedriveService.log
Output (stderr): C:\OnedriveService.log
File rotation
Replace existing... Not selected
Rotate files Selected
Rotate while running Selected
Restrict rotation to older 0 seconds
Restrict rotation to bigger 100000 bytes
Environment: Leave as default
End by selecting: Install
8. In Windows Services, located the new OneDrive service, and change the startup settings to run as <USER_NAME> and provide the password.
9. In Windows Services, start the new OneDrive service.
10. Verify that the synchronozation is still working.
11. Log out of the account or even better, restart the computer.
12. Use your automatic build setup to verify that the synchronization is still working.
And thats all, Folks.
@nightmare1942
Copy link

Please try to install the service as admin, and then afterwards open the services manager (services. msc) and change the “run as” user.

This is what I've tried also but with the same result:
[OneAuth:Error:9zj9x:00000000-0000-0000-0000-000000000000] OneAuth is not configured

@lmogthb
Copy link

lmogthb commented Aug 30, 2023

Does this still works for OneDrive for Business?

@PsychoData
Copy link

Does this still works for OneDrive for Business?

@nightmare1942 @aolmosasir Wouldn't it be nice to know how the switches worked?
I have an issue open https://github.com/MicrosoftDocs/OfficeDocs-SharePoint/issues/2905 trying to find out about what the switches for OneDrive.exe and how they work are. If you think that would be useful, it would be really helpful to do some of these

If nothing else, that issue has about 20 switches for OneDrive.exe, that seemed like they worked for me.

@Harrison-Palmer
Copy link

As of version 23.246.1127.0002 I could get this to work for OneDrive for Business with srvany and AlwaysUp. I encountered the same OneAuth:Error:9zj9x: issue using nssm, so I switched to srvany but this also was not syncing. I tried using AlwaysUp to test another method, but it wasn't syncing this way either. I had changed the OneDrive folder name and location and when you do this OneDrive shows a popup like the one below but you simply click Try Again and then you are all set. I found when I opened OneDrive again this pop up appeared again. After clicking Try Again then unchecking all notification options in OneDrive settings, I closed OneDrive and launched AlwaysUp which started syncing. I closed out of this and launched my custom service created with sryany and this was working too. I decided not to try the nssm method again because I am happy with what I have now, but if you cannot find success with nssm, try using sryany.

I have the service running as the same user as the OneDrive account and they are a local administrator on the device.

image

This was the article I used to create a service using srvany:

https://learn.microsoft.com/en-US/troubleshoot/windows-client/deployment/create-user-defined-service

@JoelLeach
Copy link

OneDrive can stop syncing after it updates itself or after a reboot, and it can seem very intermittent. If that happens, here are some troubleshooting steps you can try:

  1. Stop the OneDrive Sync service. You may also have to End Task on nssm.exe from the Windows Task Manager.
  2. If you find that Sharepoint is running in the Task Manager and using a lot of CPU, you may have to End Task on it as well.
  3. Start OneDrive manually and let it run for a while to sync any pending files.
  4. Quit OneDrive after the sync is complete.
  5. With Task Manager open to the Details page, start the OneDrive Sync service. You should see nssm.exe and OneDrive.exe open. In some cases, OneDrive may immediately close after being started. If this happens, stop the service (which should close nssm.exe) and start it again. OneDrive.exe may stay open this time.
  6. Create a test.txt file in the OneDrive folder and check the OneDrive web site to make sure it syncs. It may take a few minutes for OneDrive to notice the new file after starting the service. Then delete the test.txt file and confirm deletion on the web site.

If this doesn't work, you can try: nssm edit "OneDrive Sync" to update the service entry. If that still doesn't help, try removing and rebuilding the service in nssm.

There have been some releases of OneDrive that simply will not sync when run as a service. You have to hope the next release will work. In the meantime, you can open OneDrive manually and stay logged in to the server. Hope this helps some folks.

@jbailey0503
Copy link

jbailey0503 commented Feb 29, 2024

I am getting this error when the service tries to open OneDrive -

Program xx\Microsoft\OneDrive\OneDrive.exe for service OneDriveSync exited with return code 3765269347.

It seems they don't want this to work anymore.

@f-steff
Copy link
Author

f-steff commented Mar 4, 2024

Thank you for the detailed troubleshooting steps, @JoelLeach - they are very valuable!

Unfortunately I'm personally unable to support this further, but I'm happy that this is still useful for a lot of people - and even more happy that people like you and others are able to support it further. Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment