Skip to content

Instantly share code, notes, and snippets.

@f-steff
Last active June 13, 2024 07:48
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.
@f-steff
Copy link
Author

f-steff commented Jun 29, 2021

@joes079
The Onedrive configuration is stored with the windows user that creates the setup - it's not linked to the login account(s) you sync with Onedrive. So you need to ensure that the same user that created the setup is used to run the Onedrive service. You can use any account, as long as you are able to get it to sync during the normal windows GUI operation.

@odeds36
Copy link

odeds36 commented Aug 15, 2021

I've run the above steps on several machines: Win 10, Win 10 in WVD config, Win Server 2019 and I cannot make the OneDrive win service syncing the files stored in the OneDrive folder/site of same account (I used in the setup process)... I've run it many times (made a complete cleanup of all evidences of OneDrive including Reg keys etc. at the client/server amchine and then re-setup everything from scratch) while ensuring I'm following step by step the above instructions and, besides one time I could make it to work in the 'regular' Win 10 machine (which I cannot reproduce since then...), it simply doesn't work for me.

Has anyone faced with similar issue/s and have any idea how to make it work eventually?

@f-steff
Copy link
Author

f-steff commented Aug 16, 2021

@odeds36

I've not faced similar problems, and unfortunately debugging Onedrive (was intentionally made hard) isn't easy.
Try verifying step by step to try to locate what goes wrong:

Before you attempt to run it as a service, is the Onedrive application correctly working in the first place when logged in as user XYZ?

Once you have verified it's working, then ensure it's not set to autostart and quit/kill the Onedrive application. Then still while logged in as user XYZ, try to start the Onedrive service configured to run as user XYZ. It should still be working.

Then try to reboot the PC. Before you log in as any user, attempt to verify if the Onedrive service is still working. (Filezilla as a service is an excellent method to test). It should still be working.

Then try to log in as any user, and verify it's still working. If it's not working, there's probably an Autostarting Onedrive application that needs to have autostart disabled. (Note: This may have screwed up the settings of the service,) Disable it and start over.

Good luck

@nicoh88
Copy link

nicoh88 commented Oct 29, 2021

@f-steff Works great! Thanks.


Caution in company networks, with Internet access of user level with Fortigate FSSO/FSAE. If the service is started as a user and the user has Internet access through Fortigate FSSO/FSAE, these rights are removed after approx. 5-15 minutes due to the normally service logon. Result, after 5-15 minutes there is no more sync via OneDrive.

Solution: Firewall exception for the IP of the server / client.

  • Port: HTTPS / 443
  • OneDrive business IP ranges: 13.107.136.0/22, 40.108.128.0/17, 52.104.0.0/14, 104.146.128.0/17

Bildschirmfoto 2021-10-29 um 07 29

@hatjan
Copy link

hatjan commented Feb 27, 2022

Could I get some help, please? I would love to use this feature but I don't get it to work.

  • The previously installed Outlook Desktop App (version 2022) on my Windows 2019 Server ran correctly and I disabled the app from launching.
  • I installed the service as described.
  • I can start the newly created service, but:
    • the logfile remains empty
    • no synchronization, files created on other PCs do not arrive, files created on the server remain in sync status (two rond arrows).

I've performed the installation several times but alway with the same result.
I found a simuiar problem in the posts above but no solution for my situation.
Thanks in advance!

@jaxnz
Copy link

jaxnz commented May 21, 2022

Just confirming this works well on WS2022 Azure Data Centre Edition.

We do have one issue, we use the OneDrive Sync client as a one-way sync to a SharePoint Library (SharePoint API had small non-frequent issues, but we needed more reliability. OneDrive Sync Client seems to manage these issues itself, so resolves the problem).

The issue is, we are generating between 500-5000 documents which needs to sync each time. It syncs the first few hundred, but then stops. We have build in a process that checks the generated file against SharePoint to see if it has sync'd - if not it restarts the service, and continues this process until complete.

Has anyone else countered this issue?

@jbfabbe
Copy link

jbfabbe commented Oct 18, 2022

@gmkado @f-steff I have exact he same issue. When deleting a file from onedrive on another workstatioon, it will not delete it via the service running on the Fileserver. When I stop the service and start onedrive manually, it deletes it immediately.
The service is started with the same user

@xboxscanner
Copy link

Im gettting errors
OneAuth:Error:9zj9x:00000000-0000-0000-0000-000000000000] OneAuth is not configured
the account used to log in to onedrive is a std user account

@pgsdapest
Copy link

I'm getting this same error @xboxscanner. Really need help. Ran installer as described, confirmed that onedrive was running in task manager yet no files were being synced between server and online site. @xboxscanner does the 365 account you use happen to require 2fa?

@SirWobbyTheFirst
Copy link

I'm encountering a different error to @xboxscanner and @jbfabbe but I do see the same error message in the NSSM log files produced. Mine is that OneDrive has updated to version 22 and is now built on WinUI 3 meaning it pulls in Windows.UI.Xaml.dll and when run as a service it immediately crashes and the Event Viewer shows a it crashes from that module and it is the same 0xC27B error code.

If I run OneDrive in a normal user session, with a File Explorer and desktop, it starts up fine, so it clearly needs something UI related now whereas the previous version worked fine, The account I'm using is a standard user account as well. So if anyone has any ideas on how to revert OneDrive back to a previous version or fix this issue, I'm all ears.

@sergue2
Copy link

sergue2 commented Jan 25, 2023

Same OneAuth Error here...

@pgsdapest
My 365 Account doesn't require 2fa but still don't work

@ashaman99
Copy link

Same OneAuth error. Was working until I had to stop and restart the service in early Jan. Cannot get it to sync when running the service, but works fine when running the app as normal.

@SirWobbyTheFirst
Copy link

So far, I've had to just revert back to using AutoLogon from Sysinternals to make the virtual machine running OneDrive to login as the service account I'm using automatically and start OneDrive once logged in. It sucks but it will do for now.

@bitcomgit
Copy link

this was working fine for a few years, unfortunately since last week the service starts but does not sync data.

@KalnaiK
Copy link

KalnaiK commented Mar 28, 2023

Has anyone found a solution to this problem?
This work fine since last week.

@bitcomgit
Copy link

bitcomgit commented Mar 28, 2023 via email

@mpistora
Copy link

Good news:
at https://www.coretechnologies.com/blog/alwaysup/onedrive-version-23-48-problem it is described that OneDrive version 23.073.0404.0001 or later works again.

@mpistora
Copy link

Bad news:
the problem described by @gmkado persists. Deleting files on the web does not sync to the PC. "Always keep on the device" option does not solve this.

https://www.coretechnologies.com/blog/onedrive/windows-service-not-deleting-files/#resolution describes that the problem occurs if OneDrive is running with elevated privileges. That's how it's started if it's started as a service via NSSM. For their paid AlwaysUp tool, they added the option:
Launch the application without admin rights
I would need a similar option for NSSM.

@SirWobbyTheFirst
Copy link

Unfortunately, it's still broken for me even on 23.076.0409.0001 and produces the same error message as it did when the issue started. NSSM tries to start OneDrive and then OneDrive immediately crashes with the following dumped into the Application log. So I'm still going to have to continue with having my Server 2022 VM automatically login and then start OneDrive. Why couldn't they just leave it bloody well alone, OneDrive does not need a fancy UI, it just needs basic Win32 controls. Leave it alone.

Faulting application name: OneDrive.exe, version: 23.76.409.1, time stamp: 0x5cbc5e59
Faulting module name: Windows.UI.Xaml.dll, version: 10.0.20348.617, time stamp: 0x5ad50d66
Exception code: 0xc000027b
Fault offset: 0x0000000000814788
Faulting process id: 0x17b8
Faulting application start time: 0x01d97bbb4cb02cf3
Faulting application path: C:\Program Files\Microsoft OneDrive\OneDrive.exe
Faulting module path: C:\Windows\System32\Windows.UI.Xaml.dll
Report Id: 9bad14ba-9636-49cf-b4f0-74a0fd5fc19e
Faulting package full name:
Faulting package-relative application ID:

@f-steff
Copy link
Author

f-steff commented May 7, 2023

@mpistora

** I would need a similar option for NSSM.

Isn’t this just to run the service as a non-admin user on the machine?
NSSM should have no problem doing that.

I’m unfortunately not able to test any of this, but please let us know if it’s working!

@nightmare1942
Copy link

nightmare1942 commented May 8, 2023

Unfortunately not:
image

And there is no option to run it inside user context.

@mpistora
Copy link

mpistora commented May 9, 2023

Thanks for the interesting idea and for testing.
Our process is that we generate files on a Windows server, OneDrive sync transfers them to SharePoint, where they are deleted after processing. We need to sync the deletion back to the Windows server.

@f-steff
Copy link
Author

f-steff commented May 16, 2023

And there is no option to run it inside user context.

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

@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

@aolmosasir
Copy link

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