'How do I run some lines in a batch script as admin but others as user?
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
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anders |
