简体   繁体   中英

Windows batch file start another batch with elevated rights

I want my batch file start another batch file with elevated (admin) rights without having to accept a prompt. I have an user (called "user") with admin rights, but if I try:

runas /savecred /user:user "program.bat"

The rights won't get elevated. But if I try:

runas /savecred /user:administrator "program.bat"

and enter the password of "user" it says: Unknown user or wrong passphrase.

So: What do I have to do to elevate the rights only with the "user"-account?

This might be a bit overkill but if the second batch file is supposed to be always run as administrator you could paste following code at the start of it:

@echo off & setlocal EnableDelayedExpansion
IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
    >NUL 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
    >NUL 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)
IF '%errorlevel%' NEQ '0' (
    echo Asking for administrative rights
    goto uac
) ELSE ( goto adm )
:uac
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\uac.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\uac.vbs"
"%temp%\uac.vbs"
del "%temp%\uac.vbs"
cls
echo Did not get administrative rights
exit
:adm
pushd "%CD%"
CD /D "%~dp0"
cls
echo Got administrative rights

::
:: YOUR CODE HERE
::

EDIT: It won't show the prompt and give you admin rights when you disable UAC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM