在excel VBA中使用Powershell启用Windows Defender

在excel VBA中使用Powershell启用Windows Defender,excel,vba,powershell,Excel,Vba,Powershell,在以下代码中,我尝试使用powershell启用Windows Defender 子启用\u禁用\u Windows\u Defender\u使用\u PowerShell 将wshShell设置为对象 Dim wshShellExec作为对象 Dim strCommand As字符串 Rem Enable=false-Disable=true strCommand=Powershell-nologo-WindowStyle Hidden-ExecutionPolicy Bypass-Comma

在以下代码中,我尝试使用powershell启用Windows Defender

子启用\u禁用\u Windows\u Defender\u使用\u PowerShell 将wshShell设置为对象 Dim wshShellExec作为对象 Dim strCommand As字符串 Rem Enable=false-Disable=true strCommand=Powershell-nologo-WindowStyle Hidden-ExecutionPolicy Bypass-Command Set MpPreference-DisableRealtimeMonitoring$false 设置wshShell=CreateObjectWScript.Shell 设置wshShellExec=wshShell.ExecstrCommand 端接头 执行代码时没有错误,但我没有启动Windows Defender 有什么想法吗

我试过了,但对我也不起作用

子启用\u禁用\u Windows\u Defender\u使用\u PowerShell 将wshShell设置为对象 Dim wshShellExec作为对象 Dim strCommand As字符串 Rem Enable=false-Disable=true strCommand=Powershell-nologo-命令启动进程Powershell-动词运行方式 设置wshShell=CreateObjectWScript.Shell 设置wshShellExec=wshShell.ExecstrCommand strCommand=Powershell-nologo-WindowStyle Hidden-ExecutionPolicy Bypass-Command Set MpPreference-DisableRealtimeMonitoring$false 设置wshShell=CreateObjectWScript.Shell 设置wshShellExec=wshShell.ExecstrCommand 端接头 你可以试试这些:

禁用Windows Defender

启用Windows Defender


您需要以管理员身份运行此代码,至于如何执行此操作,我同意您应该为此发布一个新问题。

为什么不在powershell中尝试此代码,而不是从VBScript中尝试?[grin]我会运行vbscript,windows defender会阻止我。。这就是为什么我正在寻找一种方法来跳过禁用它,然后运行我的脚本,然后再次启用它。请尝试在powershell ISE中运行powershell代码,看看会发生什么情况,尤其是出现什么错误。这显示了如何使PoSh以管理员身份运行或在提升会话中运行>>>使用powershell以管理员身份运行命令?-堆栈溢出-我建议你开始一个新问题,因为这个问题已经偏离了标题中的主题。然后,在这个新问题中,列出您所做的和失败的,以及所有错误消息。非常感谢。。但是我如何在excel VBA中使用这样的代码呢?@YasserKhalil您要求使用Powershell启用Windows Defender,所以这是Powershell..我已经在主要帖子中放置了excel VBA标记,但我不知道是谁删除了它们。。一般来说,我已经发布了另一个链接
$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
$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