简体   繁体   English

在Excel VBA中使用Powershell启用Windows Defender

[英]Enable Windows Defender using Powershell in excel VBA

In the following code, I am trying to enable the Windows Defender using powershell 在以下代码中,我尝试使用powershell启用Windows Defender

Sub Enable_Disable_Windows_Defender_Using_PowerShell()
    Dim wshShell        As Object
    Dim wshShellExec    As Object
    Dim strCommand      As String

    Rem Enable = false - Disable = true
    strCommand = "Powershell -nologo -WindowStyle Hidden -ExecutionPolicy Bypass -Command ""Set-MpPreference -DisableRealtimeMonitoring $false"""
    Set wshShell = CreateObject("WScript.Shell")
    Set wshShellExec = wshShell.Exec(strCommand)
End Sub

There is no error at executing the code but I didn't get the Windows Defender Eabled Any idea ..? 在执行代码时没有错误,但是我没有Windows Defender Eabled任何想法..?

I tried this but doesn't work for me too 我尝试了这个但对我也不起作用

Sub Enable_Disable_Windows_Defender_Using_PowerShell()
    Dim wshShell        As Object
    Dim wshShellExec    As Object
    Dim strCommand      As String

    Rem Enable = false - Disable = true

    strCommand = "Powershell -nologo -Command ""Start-Process powershell -Verb runAs"""
    Set wshShell = CreateObject("WScript.Shell")
    Set wshShellExec = wshShell.Exec(strCommand)


    strCommand = "Powershell -nologo -WindowStyle Hidden -ExecutionPolicy Bypass -Command ""Set-MpPreference -DisableRealtimeMonitoring $false"""
    Set wshShell = CreateObject("WScript.Shell")
    Set wshShellExec = wshShell.Exec(strCommand)
End Sub

You could try these: 您可以尝试以下方法:

Disable Windows Defender 禁用Windows Defender

$regpath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows Defender"
if (!(Test-Path $regpath -PathType Container)) {
    New-Item -Path $regpath -ItemType Container -Force
}
Set-ItemProperty -Path $regpath -Name "DisableAntiSpyware" -Value 1 -Type DWord -Force
# stop the service and set it to Disabled
Stop-Service -Name WinDefend -Confirm:$false -Force
Set-Service -Name WinDefend -StartupType Disabled

Enable Windows Defender 启用Windows Defender

$regpath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows Defender"
if (Test-Path $regpath -PathType Container) {
    Set-ItemProperty -Path $regpath -Name "DisableAntiSpyware" -Value 0 -Type DWord -Force
    # or remove the whole registry key "Windows Defender"
    # Remove-Item -Path $regpath -Force
}
# set the service to startup Automatic and start the service
Set-Service -Name WinDefend -StartupType Automatic
Start-Service -Name WinDefend -Confirm:$false

You need to run this as Administrator and as to how to do this, I agree with Lee_Daily you should post a new question for that. 您需要以管理员身份运行它,以及如何执行此操作,我同意Lee_Daily,您应该为此发布一个新问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM