AI:
RegClassLock.bat
- @echo off
- setlocal EnableExtensions
- :: Auto elevate
- fltmc >nul 2>&1
- if errorlevel 1 (
- powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
- exit /b
- )
- cls
- echo ==========================================
- echo HKCR Extension Lock Tool
- echo ==========================================
- echo.
- set /p "EXT=Input extension, for example .xxx: "
- if "%EXT%"=="" exit /b
- :: Automatically add "." if omitted
- if not "%EXT:~0,1%"=="." set "EXT=.%EXT%"
- echo.
- echo Target:
- echo HKCR\%EXT%
- echo.
- echo [1] Lock
- echo [2] Unlock
- echo [0] Exit
- echo.
- choice /c 120 /n /m "Select: "
- if errorlevel 3 exit /b
- if errorlevel 2 goto unlock
- if errorlevel 1 goto lock
- :lock
- cls
- echo Locking HKCR\%EXT% ...
- echo.
- :: HKCR is merged from these two locations
- reg add "HKCU\Software\Classes\%EXT%" /f >nul 2>&1
- reg add "HKLM\Software\Classes\%EXT%" /f >nul 2>&1
- powershell -NoProfile -Command "$ErrorActionPreference='Stop';try{$deny=[System.Security.AccessControl.RegistryRights]([int][System.Security.AccessControl.RegistryRights]::SetValue -bor [int][System.Security.AccessControl.RegistryRights]::CreateSubKey -bor [int][System.Security.AccessControl.RegistryRights]::Delete);$open=[System.Security.AccessControl.RegistryRights]([int][System.Security.AccessControl.RegistryRights]::ReadPermissions -bor [int][System.Security.AccessControl.RegistryRights]::ChangePermissions);$sid=New-Object System.Security.Principal.SecurityIdentifier('S-1-1-0');$rule=New-Object System.Security.AccessControl.RegistryAccessRule($sid,$deny,[System.Security.AccessControl.InheritanceFlags]::ContainerInherit,[System.Security.AccessControl.PropagationFlags]::None,[System.Security.AccessControl.AccessControlType]::Deny);foreach($base in @([Microsoft.Win32.Registry]::CurrentUser,[Microsoft.Win32.Registry]::LocalMachine)){$k=$base.OpenSubKey('Software\Classes\%EXT%',[Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,$open);$acl=$k.GetAccessControl();$acl.RemoveAccessRuleSpecific($rule)|Out-Null;$acl.AddAccessRule($rule);$k.SetAccessControl($acl);$k.Close()};exit 0}catch{Write-Host $_;exit 1}"
- if errorlevel 1 (
- echo.
- echo [ERROR] Lock failed.
- ) else (
- echo.
- echo [OK] HKCR\%EXT% is locked.
- echo.
- echo Blocked:
- echo - Set or modify values
- echo - Create subkeys
- echo - Delete the key
- )
- echo.
- pause
- exit /b
- :unlock
- cls
- echo Unlocking HKCR\%EXT% ...
- echo.
- powershell -NoProfile -Command "$ErrorActionPreference='Stop';try{$deny=[System.Security.AccessControl.RegistryRights]([int][System.Security.AccessControl.RegistryRights]::SetValue -bor [int][System.Security.AccessControl.RegistryRights]::CreateSubKey -bor [int][System.Security.AccessControl.RegistryRights]::Delete);$open=[System.Security.AccessControl.RegistryRights]([int][System.Security.AccessControl.RegistryRights]::ReadPermissions -bor [int][System.Security.AccessControl.RegistryRights]::ChangePermissions);$sid=New-Object System.Security.Principal.SecurityIdentifier('S-1-1-0');$rule=New-Object System.Security.AccessControl.RegistryAccessRule($sid,$deny,[System.Security.AccessControl.InheritanceFlags]::ContainerInherit,[System.Security.AccessControl.PropagationFlags]::None,[System.Security.AccessControl.AccessControlType]::Deny);foreach($base in @([Microsoft.Win32.Registry]::CurrentUser,[Microsoft.Win32.Registry]::LocalMachine)){$k=$base.OpenSubKey('Software\Classes\%EXT%',[Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,$open);if($k){$acl=$k.GetAccessControl();$acl.RemoveAccessRuleSpecific($rule)|Out-Null;$k.SetAccessControl($acl);$k.Close()}};exit 0}catch{Write-Host $_;exit 1}"
- if errorlevel 1 (
- echo.
- echo [ERROR] Unlock failed.
- ) else (
- echo.
- echo [OK] HKCR\%EXT% is unlocked.
- )
- echo.
- pause
- exit /b
复制代码
初步测试通过。 |