728x90
:-----------------------------------------------------------------------------------------------------------------------------
REM Check administrator privileges
REM ref : https://bebhionn.tistory.com/52
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM If error flag is set, we don't have admin rights
if '%errorlevel%' NEQ '0' (goto UACPrompt) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    REM When running with admin rights, default path changes, so change it to bat file path (%~dp0)
    CD /D "%~dp0"
:-----------------------------------------------------------------------------------------------------------------------------

참고 : https://bebhionn.tistory.com/52

 

Batch 파일 관리자 권한으로 실행 코드

batch 파일을 만들때 오른쪽 -> 관리자 권한으로 실행 으로 안하고 바로 실행해도 관리자 권한을 얻어오고 싶을 때 사용하는 코드입니다. 제일 앞에 기입해야 하며 @echo off가 있을 시 @echo off 뒤에

bebhionn.tistory.com

 

728x90

'Script > bat' 카테고리의 다른 글

Bat - MSBuild/VsDevCmd의 경로를 가져오기.  (0) 2025.04.13
728x90

CI/CD를 동작시킬 때 MSBuild를 사용하는 경우가 있을 것이다.

이 때 개발환경과 CI/CD환경이 다를 것이고 경로도 다를 것이다.

그래서 Build.bat 파일과 같이 bat파일로 동작시키는 경우가 있다.

 

MSBuild의 경로 가져오기

:-----------------------------------------------------------------------------------------------------------------------------
REM Find Visual Studio 2022 installation
set MSBUILD_PATH=

REM Try to find Visual Studio installation using vswhere
set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
for /f "usebackq tokens=*" %%i in (`"%VSWHERE%" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do (
    set "MSBUILD_PATH=%%i\MSBuild\Current\Bin\MSBuild.exe"
    goto :found_msbuild
)

:found_msbuild
if "%MSBUILD_PATH%"=="" (
    echo Error: MSBuild.exe not found.
    echo Please install Visual Studio 2022 and try again.
    pause
    exit /B 1
)
:-----------------------------------------------------------------------------------------------------------------------------

:-----------------------------------------------------------------------------------------------------------------------------
echo Using MSBuild at: %MSBUILD_PATH%

REM "%MSBUILD_PATH%" Engine.sln -p:Configuration="Debug" -p:Platform=Win32
REM "%MSBUILD_PATH%" Engine.sln -p:Configuration="Release" -p:Platform=Win32

"%MSBUILD_PATH%" Engine.sln -p:Configuration="Debug" -p:Platform=x64
REM "%MSBUILD_PATH%" Engine.sln -p:Configuration="Release" -p:Platform=x64
:-----------------------------------------------------------------------------------------------------------------------------

 

VsDevCmd의 경로 가져오기

나의 경우에는 Visual Studio 2022를 사용하고 있기 때문에 VS2022_PATH라고 했을 뿐이다.

이렇게하면 설치된 최신 버전의 Visual Studio 의 VsDevCmd.bat 경로를 가져올 것이다.

:-----------------------------------------------------------------------------------------------------------------------------
echo.
echo [Execute VsDevCmd.bat]
REM Find Visual Studio 2022 installation
set VS2022_PATH=

REM Try to find Visual Studio installation using vswhere
set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
for /f "usebackq tokens=*" %%i in (`"%VSWHERE%" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do (
    set "VS2022_PATH=%%i\Common7\Tools\VsDevCmd.bat"
    goto :found_vs
)

:found_vs
if "%VS2022_PATH%"=="" (
    echo Error: Visual Studio 2022 not found.
    echo Please install Visual Studio 2022 and try again.
    pause
    exit /B 1
)

echo Using Visual Studio 2022 at: %VS2022_PATH%
call "%VS2022_PATH%"
:-----------------------------------------------------------------------------------------------------------------------------
728x90

'Script > bat' 카테고리의 다른 글

Bat - 관리자 권한으로 실행하기  (0) 2025.04.13

+ Recent posts