Batch Script To Run As Administrator
Answer :
Solutions that did not work
No: The - create a shortcut [ -> Compatibility -> "run this program as an administrator" ] solution does not work.
This option is greyed out in Windows 7. Even with UAC disabled
No: The runas /env /user:domain\Administrator <program.exe/command you want to execute>
is also not sufficient because it will prompt the user for the admin password.
Solution that worked
Yes: Disable UAC -> Create a job using task scheduler, this worked for me.
- Create a job under task scheduler and make it run as a user with administrator permissions.
- Explicitly mark: "Run with highest privileges"
- Disable UAC so there will be no prompt to run this task
You can let the script enable UAC afterwards by editing the registry if you would want. In my case this script is ran only once by creation of a windows virtual machine, where UAC is disabled in the image.
Still looking forward for the best approach for a script run as admin without too much hassle.
@echo off call :isAdmin if %errorlevel% == 0 ( goto :run ) else ( echo Requesting administrative privileges... goto :UACPrompt ) exit /b :isAdmin fsutil dirty query %systemdrive% >nul exit /b :run <YOUR BATCH SCRIPT HERE> exit /b :UACPrompt echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" echo UAC.ShellExecute "cmd.exe", "/c %~s0 %~1", "", "runas", 1 >> "%temp%\getadmin.vbs" "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" exit /B`
its possible using syntax:
RUNAS [/profile] [/env] [/netonly] /user:user Program Key : /profile Option to load the user's profile (registry) /env Use current environment instead of user's. /netonly Use the credentials specified only for remote connections. /user Username in form USER@DOMAIN or DOMAIN\USER (USER@DOMAIN is not compatible with /netonly) Program The command to execute
example :
runas /env /user:domain\Administrator <program.exe/command you want to execute>
Comments
Post a Comment