Powershell从防御者列表中排除文件夹的方法

Manly, now you can stop reading, go to the PowerShell and update excludes:)

男子气概,现在您可以停止阅读,转到PowerShell并更新排除项:)

But if you are want to see how to perform this in more detail — welcome under the habrcut.

但是,如果您想了解如何更详细地执行此操作,请在快捷方式下进行。

UPD: Added a script to add selected folder to excluded Defender locations (at the end of the post)

UPD:添加了一个脚本,用于将选定的文件夹添加到排除的Defender位置(在文章末尾)

For the beginning, let's go through the current excluded folders list.

首先,让我们浏览当前排除的文件夹列表。

To perform this operation we need to open the PowerShell session with admin rights and execute next commands

要执行此操作,我们需要以管理员权限打开PowerShell会话并执行下一个命令

$defenderSettings = Get-MpPreference
$defenderSettings.ExclusionPath
image

As you see Rider and Pycharm already added some folders on my machine

如您所见,Rider和Pycharm已经在我的机器上添加了一些文件夹

Let's remove the specified repositories folders and make the rule more generic:

让我们删除指定的存储库文件夹,并使规则更通用:

image

Line by line:

逐行:

$currentItems=@($defenderSettings.ExclusionPath)

Store our folders list to a new variable

将我们的文件夹列表存储到新变量

$currentItems

Asserting the list is copied.

声明列表已复制。

$newItems = $currentItems -notlike "C:\Users\sync\Pycharm"
$newItems = $newItems -notlike "C:\Users\sync\Rider"
$newItems = $newItems -notlike "D:\Sources\B*"

Removes «C:\Users\sync\Pycharm» «C:\Users\sync\Rider» and «D:\Sources\B*» folders from the list. You probably will have some another pattern.

从列表中删除«C:\ Users \ sync \ Pycharm»«C:\ Users \ sync \ Rider»和«D:\ Sources \ B *»文件夹。 您可能还会有其他模式。

$newItems

Verifying the resulting list.

验证结果列表。

As a more generic folders filtering could be used something like this

作为更通用的文件夹过滤,可以使用如下所示的内容

$newItems = $currentItems -notlike "C:\Users*\Pycharm"

but double check before moving to the next step.

但在进入下一步之前请仔细检查。

Adding new folders to the excluded list:

将新文件夹添加到排除列表中:

As we are removing the paths added by rider and Pycharm the environment can start working slower. So it's the time to add the folders with our sources.

当我们删除骑手和Pycharm添加的路径时,环境开始运行会变慢。 现在是时候添加带有源文件的文件夹了。

image
$updatedList=$newItems
$updatedList += "C:\Users\sync\PycharmProjects\"
$updatedList += "C:\Users\sync\RiderProjects\"
$updatedList += "D:\Sources\"
$updatedList += "C:\Users\sync\source\repos"
$updatedList

The first 2 lines intended to add Rider and Pycharm projects.

前两行旨在添加Rider和Pycharm项目。

Then adding our additional source folders path.

然后添加其他源文件夹路径。

And in the latest command just verifying the resulting list.

而在最新命令中,只需验证结果列表即可。

And now time to save new folders to defender exclude list:

现在是时候将新文件夹保存到防御者排除列表了:

image

The first command just to double-check the folders collection.

第一个命令只是要仔细检查文件夹集合。

Store our changed exclude list to Defender:

将更改后的排除列表存储到Defender:

Set-MpPreference -ExclusionPath $updatedList

And verifying the result:

并验证结果:

Get-MpPreference | %{$_.ExclusionPath}

Perfest! Defender will never cause again the IDE freezes

完美! Defender将再也不会导致IDE冻结

奖金! 将当前文件夹添加到排除列表的脚本: (Bonus! Script to add current folder to excludes list:)
$defenderSettings = Get-MpPreference
$currentItems=@($defenderSettings.ExclusionPath)
$currentItems +=$pwd.Path
$currentItems
Set-MpPreference -ExclusionPath $currentItems

Now you can save this code under the project folder and ship with your sources.

现在,您可以将此代码保存在项目文件夹下,并随源一起提供。

示范工作 (Demonstation of work)

翻译自: https://habr.com/en/post/500814/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值