0

I have this small script to restart OneDrive to fix problems that occur. I want to run it as admin, to ensure that it has the permissions to do its job, but OneDrive cant be started as admin. Can i start the sript ad administrator, but start OneDrive as a normal User? Heres the sript:

taskkill /IM onedrive.exe /F
sc stop OneSyncSvc_4a414
sc stop FileSyncHelper
timeout /t 2 /nobreak >nul
sc start OneSyncSvc_4a414
sc start FileSyncHelper
cd C:\Program Files\Microsoft OneDrive
OneDrive.exe
exit
Ven0m0
  • 1

1 Answers1

0

A process is elevated or not, you can't go back and forth. Batch files are executed by a cmd.exe process.

You would have to elevate the batch file to do some of the tasks, then the non-elevated tasks in the original context/process:

@echo off
if "%1"=="admin" goto admin
rem start yourself elevated here with admin as a parameter
timeout ...
start onedrive.exe
@goto :EOF

:admin
sc ...

In your specific case, you should try to fix OneDrive instead of hacking around the problem. Or alternatively, change the security on the service so a normal user can start/stop it.

Anders
  • 97,548
  • 12
  • 110
  • 164