@echo off
chcp 65001 >nul
title Windows 安全管控脚本
color 0A

echo ========================================
echo   正在检查管理员权限...
echo ========================================
net session >nul 2>&1
if %errorLevel% neq 0 (
    color 0C
    echo [错误] 请务必右键点击此文件，选择“以管理员身份运行”！
    pause
    exit /b
)

echo [权限检查通过] 开始应用限制...
echo.

:: 新增设置作用于运行脚本的当前账户（HKCU）；请在目标账户内提升权限运行。
:: 如用其他管理员凭据运行，HKCU 设置将作用于该管理员账户。
setlocal
set "AUDIT_NEW_FAILED=0"
set "AUDIT_PS=%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"
if exist "%SystemRoot%\Sysnative\WindowsPowerShell\v1.0\powershell.exe" set "AUDIT_PS=%SystemRoot%\Sysnative\WindowsPowerShell\v1.0\powershell.exe"

:: 1. 屏保等待 60 秒，恢复时显示登录界面；指定空白屏保，避免“无屏保”导致策略不生效。
echo [1/7] 设置一分钟屏保及恢复时重新登录...
if not exist "%SystemRoot%\System32\scrnsave.scr" (
    echo [错误] 未找到 Windows 空白屏保 scrnsave.scr。
    set "AUDIT_NEW_FAILED=1"
)
call :AuditReg "HKCU\Control Panel\Desktop" ScreenSaveActive REG_SZ 1
call :AuditReg "HKCU\Control Panel\Desktop" ScreenSaveTimeOut REG_SZ 60
call :AuditReg "HKCU\Control Panel\Desktop" ScreenSaverIsSecure REG_SZ 1
call :AuditReg "HKCU\Control Panel\Desktop" SCRNSAVE.EXE REG_SZ "%SystemRoot%\System32\scrnsave.scr"
call :AuditReg "HKCU\Software\Policies\Microsoft\Windows\Control Panel\Desktop" ScreenSaveActive REG_SZ 1
call :AuditReg "HKCU\Software\Policies\Microsoft\Windows\Control Panel\Desktop" ScreenSaveTimeOut REG_SZ 60
call :AuditReg "HKCU\Software\Policies\Microsoft\Windows\Control Panel\Desktop" ScreenSaverIsSecure REG_SZ 1
call :AuditReg "HKCU\Software\Policies\Microsoft\Windows\Control Panel\Desktop" SCRNSAVE.EXE REG_SZ "%SystemRoot%\System32\scrnsave.scr"
:: 消除屏保启动后的密码保护宽限期，避免刚启动屏保时可直接返回桌面。
call :AuditReg "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" ScreenSaverGracePeriod REG_SZ 0

:: 2. 锁屏背景为单张图片，关闭 Windows 聚焦、幻灯片及花絮/提示/技巧。
echo [2/7] 设置锁屏图片并关闭花絮、提示及技巧...
call :AuditReg "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" RotatingLockScreenEnabled REG_DWORD 0
call :AuditReg "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" RotatingLockScreenOverlayEnabled REG_DWORD 0
call :AuditReg "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" SubscribedContent-338387Enabled REG_DWORD 0
call :AuditReg "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Lock Screen" SlideshowEnabled REG_DWORD 0
call :AuditReg "HKCU\SOFTWARE\Policies\Microsoft\Windows\CloudContent" DisableWindowsSpotlightFeatures REG_DWORD 1
call :AuditReg "HKLM\SOFTWARE\Policies\Microsoft\Windows\Personalization" NoLockScreenSlideshow REG_DWORD 1
:: 优先使用同目录 lockscreen.jpg，否则使用 Windows 自带锁屏图片。
:: 使用 Windows 锁屏接口设置当前用户图片，避免仅写企业版图片策略在专业版上无效。
set "AUDIT_LOCK_IMAGE=%SystemRoot%\Web\Screen\img100.jpg"
if exist "%~dp0lockscreen.jpg" set "AUDIT_LOCK_IMAGE=%~dp0lockscreen.jpg"
"%AUDIT_PS%" -NoLogo -NoProfile -NonInteractive -Command "$ErrorActionPreference='Stop'; try { Add-Type -AssemblyName System.Runtime.WindowsRuntime; $null=[Windows.Storage.StorageFile,Windows.Storage,ContentType=WindowsRuntime]; $null=[Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime]; $bridge=[System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq 'AsTask' -and $_.IsGenericMethodDefinition -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation'+[char]96+'1' } | Select-Object -First 1; $op=[Windows.Storage.StorageFile]::GetFileFromPathAsync($env:AUDIT_LOCK_IMAGE); $task=$bridge.MakeGenericMethod([Windows.Storage.StorageFile]).Invoke($null,@($op)); if(-not $task.Wait(30000)){throw 'Timed out reading lock screen image'}; $file=$task.GetAwaiter().GetResult(); $task=[System.WindowsRuntimeSystemExtensions]::AsTask([Windows.System.UserProfile.LockScreen]::SetImageFileAsync($file)); if(-not $task.Wait(30000)){throw 'Timed out setting lock screen image'}; $task.GetAwaiter().GetResult(); Write-Host ('[OK] '+$env:AUDIT_LOCK_IMAGE); exit 0 } catch { [Console]::Error.WriteLine($_.Exception.Message); exit 1 }"
if errorlevel 1 (
    echo [错误] 锁屏图片设置失败，请检查图片文件、PowerShell 是否可用及组织策略限制。
    set "AUDIT_NEW_FAILED=1"
)

:: 3. Windows 10 详细状态应用选“无”，并兼容 Windows 11 锁屏小组件。
:: 以下用户偏好键在不同 Windows 版本上可能存在差异，重启后需核对设置页面。
echo [3/7] 关闭锁屏详细状态应用、通知及小组件...
call :AuditReg "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Lock Screen" DetailedStatusApplicationId REG_SZ ""
call :AuditReg "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Lock Screen" LockScreenWidgetsEnabled REG_DWORD 0
call :AuditReg "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Lock Screen" AutoSelectWidgetsOnLockScreen REG_DWORD 0
call :AuditReg "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Lock Screen" LockScreenWidgetsSystemCurationEnabled REG_DWORD 0
call :AuditReg "HKLM\SOFTWARE\Policies\Microsoft\Windows\System" DisableLockScreenAppNotifications REG_DWORD 1
echo       - 新增配置已处理，执行结果将在末尾显示。
echo.

:: 4. 禁用 USB 存储
echo [4/7] 禁用 USB 存储设备...
reg add "HKLM\SYSTEM\CurrentControlSet\Services\USBSTOR" /v Start /t REG_DWORD /d 4 /f >nul 2>&1
reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices" /v Deny_All /t REG_DWORD /d 1 /f >nul 2>&1
echo       - 完成。

:: 5. 禁用打印和刻录
echo [5/7] 禁用打印服务和光盘刻录...
net stop Spooler /y >nul 2>&1
sc config Spooler start= disabled >nul 2>&1
reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\Explorer" /v NoCDBurning /t REG_DWORD /d 1 /f >nul 2>&1
echo       - 完成。

:: 6. 禁用计算机管理
echo [6/7] 禁用计算机管理和任务管理器...
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v DisableTaskMgr /t REG_DWORD /d 1 /f >nul 2>&1
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v DisableRegistryTools /t REG_DWORD /d 1 /f >nul 2>&1
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v DisableCMD /t REG_DWORD /d 2 /f >nul 2>&1
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v NoManageMyComputerVerb /t REG_DWORD /d 1 /f >nul 2>&1
if exist "%SystemRoot%\System32\compmgmt.msc" ren "%SystemRoot%\System32\compmgmt.msc" compmgmt.msc.bak
if exist "%SystemRoot%\System32\devmgmt.msc" ren "%SystemRoot%\System32\devmgmt.msc" devmgmt.msc.bak
echo       - 完成。

:: 7. 禁用程序安装
echo [7/7] 禁用 MSI 安装和商店应用...
reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer" /v DisableMSI /t REG_DWORD /d 2 /f >nul 2>&1
reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer" /v DisableUserInstalls /t REG_DWORD /d 1 /f >nul 2>&1
reg add "HKLM\SOFTWARE\Policies\Microsoft\WindowsStore" /v RemoveWindowsStore /t REG_DWORD /d 1 /f >nul 2>&1
echo       - 完成。

:: 刷新组策略
echo.
echo 正在刷新组策略...
gpupdate /force >nul 2>&1
if errorlevel 1 (
    echo [警告] 组策略刷新失败，请检查域连接或本地策略，重启后核对。
    set "AUDIT_NEW_FAILED=1"
)

echo.
echo ========================================
color 0E
if "%AUDIT_NEW_FAILED%"=="0" (
    echo   新增屏保及锁屏配置已写入。建议重启后核对实际效果。
) else (
    color 0C
    echo   新增设置存在失败或策略刷新失败，请检查上方错误，不能判定为全部完成。
)
echo   原有 USB、打印、管理工具及安装限制已执行，请按原审计要求核对。
echo   注意：当前用户设置只适用于运行脚本的账户，不自动覆盖其他用户。
echo ========================================
pause

:: 主流程必须在子程序之前退出，防止直接落入注册表写入子程序。
set "AUDIT_EXIT=%AUDIT_NEW_FAILED%"
endlocal & exit /b %AUDIT_EXIT%

:AuditReg
reg add "%~1" /v "%~2" /t "%~3" /d "%~4" /f >nul
if errorlevel 1 (
    echo [错误] 无法写入 %~1 : %~2
    set "AUDIT_NEW_FAILED=1"
)
exit /b
