Files
openide/bin/win/defender-exclusions.ps1
Roman Shevchenko 33df479eb8 [platform] ignoring "unknown drive" problems in the Defender configuration script (IJPL-37014)
GitOrigin-RevId: fbbaef70510d71c24d054851872118e32430bde4
2024-04-24 16:04:43 +00:00

58 lines
1.5 KiB
PowerShell

<#
The script adds paths, given as parameters, to the Microsoft Defender folder exclusion list,
unless they are already excluded.
#>
#Requires -RunAsAdministrator
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
if ($args.Count -eq 0) {
Write-Host "usage: $PSCommandPath path [path ...]"
exit 1
}
try {
Import-Module Defender
# returns `$true` when a path is already covered by the exclusion list
function Test-Excluded ([string] $path, [string[]] $exclusions) {
foreach ($exclusion in $exclusions) {
try {
$expanded = [System.Environment]::ExpandEnvironmentVariables($exclusion)
$resolvedPaths = Resolve-Path -Path $expanded -ErrorAction Stop
foreach ($resolved in $resolvedPaths) {
$resolvedStr = $resolved.ProviderPath.ToString()
if ([cultureinfo]::InvariantCulture.CompareInfo.IsPrefix($path, $resolvedStr, @("IgnoreCase"))) {
return $true
}
}
} catch [System.Management.Automation.ItemNotFoundException] {
} catch [System.Management.Automation.DriveNotFoundException] {
}
}
return $false
}
$exclusions = (Get-MpPreference).ExclusionPath
if (-not $exclusions) {
$exclusions = @()
}
foreach ($path in $args) {
if (-not (Test-Excluded $path $exclusions)) {
$exclusions += $path
Write-Host "added: $path"
} else {
Write-Host "skipped: $path"
}
}
Set-MpPreference -ExclusionPath $exclusions
} catch {
Write-Host $_.Exception.Message
Write-Host $_.ScriptStackTrace
exit 1
}