//

@echo off
setlocal EnableExtensions

if "%~1"=="" (
    echo No folder path specified.
    echo Usage: "%~nx0" "folder path"   ^(or drag a folder onto this file^)
    pause
    exit /b 1
)

for %%I in ("%~1") do set "TARGET=%%~fI"

fltmc >nul 2>&1
if errorlevel 1 (
    echo Requesting administrator privileges...
    powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath '%~f0' -ArgumentList ([char]34 + $env:TARGET + [char]34) -Verb RunAs -WorkingDirectory '%~dp0'"
    exit /b
)

echo Target: "%TARGET%"
echo Changing owner to: "%USERDOMAIN%\%USERNAME%"
echo.

takeown.exe /F "%TARGET%" /R /D Y /SKIPSL
if errorlevel 1 (
    echo.
    echo Failed to change ownership.
    pause
    exit /b 1
)

echo.
echo Ownership change completed.
echo.
echo Granting modify access to: "%USERDOMAIN%\%USERNAME%"
echo.

icacls.exe "%TARGET%" /grant:r "%USERDOMAIN%\%USERNAME%:(OI)(CI)M" /T /C
if errorlevel 1 (
    echo.
    echo Failed to grant permissions.
    pause
    exit /b 1
)

echo.
echo Permission change completed.

powershell.exe -NoProfile -Command "(Get-Acl -LiteralPath $env:TARGET).Owner"

pause
exit /b 0

//

# 현재 폴더 소유자 출력
- powershell
(Get-Acl -LiteralPath (Get-Location)).Owner

- cmd.exe
powershell.exe -NoProfile -Command "(Get-Acl -LiteralPath '.').Owner"



# 폴더 소유자 변경 : administrator -> userID
$Repo = 'D:\AI\stable'
$TargetUser = 'PC\userID'

takeown.exe /F . /R /D Y
icacls.exe . /setowner "%USERDOMAIN%\%USERNAME%" /T /C  <== takeown.exe 과 같지만 사용자 지정 할 수 있음
icacls.exe . /grant:r "%USERDOMAIN%\%USERNAME%:(OI)(CI)M" /T /C


---------------------------------------
< 1개 파일 추적 테스트 >

- 소유권
(Get-Acl -LiteralPath "bak\down.flac").Owner

powershell.exe -NoProfile -Command "(Get-Acl -LiteralPath 'bak\down.flac').Owner"
icacls.exe "bak\down.flac"
- 파일 수정 권한 확인


- 사용자 PC\userID 로 변경
takeown.exe /F "bak\down.flac" 
icacls.exe "bak\down.flac" /setowner "PC\userID"
icacls.exe "bak\down.flac" /grant:r "PC\userID:M"

- 관리자 BUILTIN\Administrators 로 변경
icacls.exe "bak\down.flac" /setowner "BUILTIN\Administrators"

icacls.exe "bak\down.flac" /inheritance:d <== 상속금지
icacls.exe "bak\down.flac" /remove:g "PC\userID" <== 상속을 제거해야 권한 삭제가 됨

icacls.exe "bak\down.flac" /inheritance:e <== 상속 켜기
- 상속을 켜야 다음 명령이 작동 icacls.exe . /grant:r "%USERDOMAIN%\%USERNAME%:(OI)(CI)M" /T /C 


icacls.exe "bak\down.flac" /grant:r "BUILTIN\Administrators:M"

---------------------------------------
< 폴더 테스트 >
icacls.exe "bak" /setowner "%USERDOMAIN%\%USERNAME%" /T /C
icacls.exe "bak" /grant:r "%USERDOMAIN%\%USERNAME%:(OI)(CI)M" /T /C

반응형
Posted by codens