# 为 IntelliJ IDEA 与当前项目配置 Microsoft Defender 排除项 # 使用方式:右键此文件,选择“使用 PowerShell 运行” # 脚本会自动请求管理员权限,并跳过已经存在的排除项。 $ErrorActionPreference = "Stop" # 检查管理员权限;如果不是管理员,则请求提权后重新运行。 $currentIdentity = [Security.Principal.WindowsIdentity]::GetCurrent() $currentPrincipal = New-Object Security.Principal.WindowsPrincipal($currentIdentity) $isAdministrator = $currentPrincipal.IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator ) if (-not $isAdministrator) { Write-Host "正在请求管理员权限..." -ForegroundColor Yellow $arguments = @( "-NoProfile" "-ExecutionPolicy", "Bypass" "-File", "`"$PSCommandPath`"" ) Start-Process powershell.exe -Verb RunAs -ArgumentList $arguments exit } # 仅排除 JetBrains 缓存、依赖及构建产物。 # 这样可以提升 IDEA、Maven、Vite 的性能,同时保留对项目源码的实时保护。 $pathsToExclude = @( "C:\Users\CRK\AppData\Local\JetBrains\IntelliJIdea2023.3", "D:\AAAAA\AAA-sans\client\node_modules", "D:\AAAAA\AAA-sans\client\dist", "D:\AAAAA\AAA-sans\server\target", "D:\AAAAA\AAA-sans\server\data", "D:\AAAAA\AAA-sans\server\exports" ) try { $existingExclusions = @((Get-MpPreference).ExclusionPath) foreach ($path in $pathsToExclude) { if ($existingExclusions -contains $path) { Write-Host "已存在,跳过:$path" -ForegroundColor DarkGray continue } Add-MpPreference -ExclusionPath $path Write-Host "已添加排除项:$path" -ForegroundColor Green } Write-Host "" Write-Host "Microsoft Defender 排除项配置完成。" -ForegroundColor Cyan Write-Host "当前相关排除项:" -ForegroundColor Cyan (Get-MpPreference).ExclusionPath | Where-Object { $_ -like "C:\Users\CRK\AppData\Local\JetBrains*" -or $_ -like "D:\AAAAA\AAA-sans*" } | ForEach-Object { Write-Host " $_" } } catch { Write-Host "配置失败:$($_.Exception.Message)" -ForegroundColor Red Write-Host "请确认 Microsoft Defender 已启用,并且当前账号具有管理员权限。" -ForegroundColor Yellow exit 1 } Write-Host "" Read-Host "按 Enter 键关闭窗口"