找回密码
 加入
搜索
查看: 68|回复: 5

[系统综合] au3脚本WMI添加Defender白名单

[复制链接]
发表于 前天 11:10 | 显示全部楼层 |阅读模式
au3脚本WMI添加Defender白名单,有没有办法?deepseek好像不能运行。
#include <MsgBoxConstants.au3>

; 检查管理员权限
If Not IsAdmin() Then
    MsgBox($MB_ICONWARNING, "警告", "需要管理员权限运行!")
    ShellExecuteWait(@AutoItExe, ' /AutoIt3ExecuteScript "' & @ScriptFullPath & '"', "", "runas")
    Exit
EndIf

; 配置排除路径(支持通配符)
Local $sExcludePath = "C:\\MyApp\\*"  ; 示例路径,修改为实际需要排除的路径

; 连接WMI Defender命名空间
Local $oDefender = ObjGet("winmgmts:\\.\root\Microsoft\Windows\Defender")
If @error Then
    MsgBox($MB_ICONERROR, "错误", "无法连接WMI Defender服务,错误代码: " & @error)
    Exit
EndIf

; 创建参数对象
Local $oInParams = $oDefender.Get("MSFT_MpPreference").Methods_("AddExclusionPath").InParameters.SpawnInstance_()
$oInParams.ExclusionPath = $sExcludePath
$oInParams.ExclusionType = 1  ; 1表示排除路径,2为进程名,3为扩展名

; 执行方法添加排除项
Local $oResult = $oDefender.ExecMethod("MSFT_MpPreference", "AddExclusionPath", $oInParams)
If @error Then
    MsgBox($MB_ICONERROR, "失败", "添加排除路径失败!错误代码: " & @error)
Else
    MsgBox($MB_ICONINFORMATION, "成功", "排除路径已添加:" & $sExcludePath)
EndIf
发表于 昨天 10:28 | 显示全部楼层

    Local $Windows_Defender_Paths = '"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Defender\Exclusions\Paths"'
    Run('Z:\NanaRun_1.0_Preview2_1.0.18.0\x64\MinSudo.exe -TrustedInstaller -Privileged reg add ' & $Windows_Defender_Paths & ' /v "C:\Program Files (x86)\AutoIt3" /t REG_DWORD /d 0 /f', '', @SW_HIDE) ;写入注册表






本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 昨天 12:17 | 显示全部楼层
fybhwsx 发表于 2025-4-29 10:28
Local $Windows_Defender_Paths = '"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Defender\Exclus ...

注册表法不好用,需要重启电脑才能生效的。
发表于 昨天 15:00 | 显示全部楼层
我一直这么用的,不知道需不需要重启。注册表确实有些麻烦,需要提权工具,不知道AU3怎么实现不用提权工具,能直接写注册表。。
 楼主| 发表于 昨天 15:29 | 显示全部楼层
fybhwsx 发表于 2025-4-29 15:00
我一直这么用的,不知道需不需要重启。注册表确实有些麻烦,需要提权工具,不知道AU3怎么实现不用提权工具 ...

https://www.autoitx.com/forum.ph ... 870&highlight=runas
提权这个大神会搞。
发表于 昨天 17:32 | 显示全部楼层
忘记我啥时候AI试过一次,后面没继续折腾了
#RequireAdmin
Local $sPathToExclude = "D:\TUPortable\TUPortable.exe"
Local $sLogFile = @ScriptDir & "\DefenderError.log"

; Validate path
If Not FileExists($sPathToExclude) Then
    FileWrite($sLogFile, "错误: 路径不存在 - " & $sPathToExclude & @CRLF)
    MsgBox(16, "错误", "路径不存在: " & $sPathToExclude)
    Exit
EndIf

; Check Defender status
Local $sDefenderCheck = 'powershell -ExecutionPolicy Bypass -Command "Get-Service -Name WinDefend | Select-Object -ExpandProperty Status"'
Local $iDefenderStatus = RunWait(@ComSpec & " /c " & $sDefenderCheck & " > " & $sLogFile, "", @SW_HIDE)
Local $sStatus = FileRead($sLogFile)
If StringInStr($sStatus, "Running") = 0 Then
    FileWrite($sLogFile, "错误: Windows Defender 服务未运行" & @CRLF)
    MsgBox(16, "错误", "Windows Defender 服务未运行,可能被禁用或被其他杀毒软件接管")
    Exit
EndIf

; Add to exclusion list
Local $sPowerShellCmd = 'powershell -ExecutionPolicy Bypass -Command "Add-MpPreference -ExclusionPath ''' & $sPathToExclude & '''"'
Local $iReturn = RunWait(@ComSpec & " /c " & $sPowerShellCmd & " >> " & $sLogFile, "", @SW_HIDE)

; Check result
If $iReturn = 0 Then
    MsgBox(64, "成功", "已将 " & $sPathToExclude & " 添加到 Windows Defender 白名单!")
Else
    FileWrite($sLogFile, "错误代码: " & $iReturn & @CRLF & "命令: " & $sPowerShellCmd & @CRLF)
    MsgBox(16, "错误", "添加白名单失败,错误代码: " & $iReturn & @CRLF & "详情见: " & $sLogFile)
EndIf
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2025-4-30 12:46 , Processed in 0.150349 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表