运行批处理作为管理员一个设置文件? [英] running a set up file in a batch as an admin?

查看:143
本文介绍了运行批处理作为管理员一个设置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个批处理来运行特定的命令时,code是这样的:

I created a batch to run specific commands, the code looks like this:

cd  D:\projects\Project Stress Test\signed one\com0com\x64
setupc
pause

我想是运行 setupc 文件作为一个管理员?

我试过的runas / user:用户<名称> \\管理员命令,但它没有工作。

i tried runas /user:<Name>\administrator commands but it didnt work.

有没有简单的方法来做到这一点?

is there any easy way to do that?

推荐答案

您可以让管理员在运行层面整个脚本。下面是我在脚本使用批处理功能。

You could make the entire script run at admin level. Here is a batch function I use in my scripts.

@echo off
call :Admin xReturn true 1
echo.%xReturn%
goto End

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Admin <Return> [Needed] [Success]
:: Check for Administrator privileges and request privileges if needed.
:: NOTE: This will restart the script with Admin privs if Needed is set to true.
:::: Usage: call :Admin xReturn true
:: Return success value, if user is Admin. Default `true` if Success not set.
setlocal
set xVBUAC=%Temp%\AdminUAC.vbs
set xSuccess=true
if not "%~3"=="" set xSuccess=%~3

:: Check for Access
::net session >nul 2>&1
>nul 2>&1 "%SystemRoot%\system32\cacls.exe" "%SystemRoot%\system32\config\system"
if %ErrorLevel% EQU 0 set xAdmin=%xSuccess%

:: Execute UAC
if /i not "%xAdmin%"=="%xSuccess%" if not "%~2"=="" if /i "%~2"=="true" (
  echo Set UAC = CreateObject^("Shell.Application"^) > "%xVBUAC%"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%xVBUAC%"
    if exist "%xVBUAC%" (
        "%xVBUAC%"
        rem if %ErrorLevel% EQU 5 echo Access Denied. Launching UAC.
        del "%xVBUAC%"
    )
)
endlocal & if not "%~1"=="" set "%~1=%xAdmin%"
goto :eof

:End

这篇关于运行批处理作为管理员一个设置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆