development

항상 관리자 모드로 실행되도록 BAT 파일을 코딩하는 방법은 무엇입니까?

big-blog 2020. 6. 25. 07:30
반응형

항상 관리자 모드로 실행되도록 BAT 파일을 코딩하는 방법은 무엇입니까?


내 BAT 파일 안에이 줄이 있습니다.

"Example1Server.exe"

관리자 모드에서 이것을 실행하고 싶습니다. 박쥐 코드를 수정하여 관리자로 실행하는 방법은 무엇입니까?

이 올바른지? 따옴표를 넣어야합니까?

runas /user:Administrator invis.vbs Example1Server.exe

runas특정 사용자로 프로그램을 시작하는 데 사용 합니다.

runas /user:Administrator Example1Server.exe

다른 답변은 관리자 계정 암호를 입력해야합니다. 또한 관리자 그룹의 계정으로 실행하는 것은 관리자 참조 : Wikipedia의 UAC 와 동일하지 않습니다.

Windows 7 지침

관리자 권한으로 실행하려면 배치 파일의 바로 가기를 만드십시오.

  1. 배치 파일을 마우스 오른쪽 버튼으로 클릭하고 복사를 클릭하십시오.
  2. 바로 가기를 원하는 곳으로 이동
  3. 디렉토리의 배경을 마우스 오른쪽 버튼으로 클릭하십시오.
  4. 붙여 넣기 바로 가기 선택

그런 다음 바로 가기를 관리자로 실행하도록 설정할 수 있습니다.

  1. 바로 가기를 마우스 오른쪽 버튼으로 클릭
  2. 속성을 선택하십시오
  3. 바로 가기 탭에서 고급을 클릭하십시오.
  4. "관리자 권한으로 실행"체크 상자를 선택하십시오.
  5. 확인을 클릭하고 확인

이제 바로 가기를 두 번 클릭하면 UAC 확인을 묻는 메시지가 표시 되고 관리자 권한으로 실행 (위에서 말한 것처럼 관리자 그룹의 계정으로 실행하는 것과 다릅니다)

아래 스크린 샷을 확인하십시오

스크린 샷

참고 : 관리자 권한으로 실행하면 현재 디렉토리 (경로)가 bat 파일과 동일하지 않습니다. 이로 인해 bat 파일이 옆에있는 상대 파일을 참조하는 경우가 많습니다. 예를 들어, Windows 7에서 cur dir은 bat 파일 위치가 아닌 SYSTEM32입니다! 이를 해결하려면 , 당신은 사용해야합니다

cd "%~dp0"

또는 더 나은

pushd "%~dp0"

cur dir이 bat 파일과 동일한 경로에 있는지 확인하십시오.


Just add this to the top of your bat file:

set "params=%*"
cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || (  echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B )

It will elevate to admin and also stay in the correct directory. Tested on Windows 10.


If you can use a third party utility, here is an elevate command line utility.

This is the usage description:

Usage: Elevate [-?|-wait|-k] prog [args]
-?    - Shows this help
-wait - Waits until prog terminates
-k    - Starts the the %COMSPEC% environment variable value and
                executes prog in it (CMD.EXE, 4NT.EXE, etc.)
prog  - The program to execute
args  - Optional command line arguments to prog

convert your batch file into .exe with this tool: http://www.battoexeconverter.com/ then you can run it as administrator


You can use nircmd.exe's elevate command

NirCmd Command Reference - elevate

elevate [Program] {Command-Line Parameters}

For Windows Vista/7/2008 only: Run a program with administrator rights. When the [Program] contains one or more space characters, you must put it in quotes.

Examples:

elevate notepad.exe 
elevate notepad.exe C:\Windows\System32\Drivers\etc\HOSTS 
elevate "c:\program files\my software\abc.exe"

PS: I use it on win 10 and it works


I think I have a solution to the password problem. This single argument is truly amazing. It asks for the password once, and than never asks for it again. Even if you put it onto another program, it will not ask for the password. Here it is:

runas /user:Administrator /savecred Example1Server.exe


go get github.com/mattn/sudo

Then

sudo Example1Server.exe

  1. My experimenting indicates that the runas command must include the admin-user's domain (at least it does in my organization's environmental setup):

    runas /user:AdminDomain\AdminUserName ExampleScript.bat
    
  2. The answers provided by both Kerrek SB and Ed Greaves will execute the target file under the admin user but, if the target script is a “.bat” file, they don't actually upgrade the commands in the script to have the admin-user's administrator level permissions, at least when trying to do things like add or modify registry keys that are otherwise locked down by group policy.

    Does anyone know how to actually upgrade bat-file (CMD) commands to administrator permissions?


Use the complete physical drive\path to your Target batch file in the shortcut Properties.

This does not work in Windows 10 if you use subst drives like I tried to do at first...

참고URL : https://stackoverflow.com/questions/6811372/how-to-code-a-bat-file-to-always-run-as-admin-mode

반응형