Skip to content

Commit 45a70fd

Browse files
committed
Optimize Windows Defender exclusion script with native PowerShell #2930
Since the script to add an exclusion to Windows Defender seems overly complex, this script replaces the complex LINQ-based exclusion check with a more idiomatic and simpler version PowerShell implementation. Fixes #2930 Update WindowsDefenderConfigurator.java @fdcastel, I've updated the script to simplify it as suggested. It now directly uses `Add-MpPreference -ExclusionProcess $pathsToExclude` without the extra conditionals or set creation. Let me know if anything else needs tweaking. Thanks! cc @HannesWell
1 parent 24e8355 commit 45a70fd

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WindowsDefenderConfigurator.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,12 @@ public static String createAddExclusionsPowershellCommand(String extraSeparator)
342342
//
343343
// For .NET's stream API called LINQ see:
344344
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable
345-
String excludedPaths = paths.stream().map(Path::toString).map(p -> '"' + p + '"')
346-
.collect(Collectors.joining(',' + extraSeparator));
347-
final String exclusionType = "ExclusionProcess"; //$NON-NLS-1$
348-
return String.join(';' + extraSeparator, "$exclusions=@(" + extraSeparator + excludedPaths + ')', //$NON-NLS-1$
349-
"$existingExclusions=[Collections.Generic.HashSet[String]](Get-MpPreference)." + exclusionType, //$NON-NLS-1$
350-
"if($existingExclusions -eq $null) { $existingExclusions = New-Object Collections.Generic.HashSet[String] }", //$NON-NLS-1$
351-
"$exclusionsToAdd=[Linq.Enumerable]::ToArray([Linq.Enumerable]::Where($exclusions,[Func[object,bool]]{param($ex)!$existingExclusions.Contains($ex)}))", //$NON-NLS-1$
352-
"if($exclusionsToAdd.Length -gt 0){ Add-MpPreference -" + exclusionType + " $exclusionsToAdd }"); //$NON-NLS-1$ //$NON-NLS-2$
345+
String excludedPaths = paths.stream().map(Path::toString).map(p -> "'" + p.replace("'", "''") + "'")
346+
.collect(Collectors.joining(','));
347+
return String.join(";",
348+
"$pathsToExclude = @(" + excludedPaths + ")",
349+
"Add-MpPreference -ExclusionProcess $pathsToExclude"
350+
);
353351
}
354352

355353
private static void excludeDirectoryFromScanning(IProgressMonitor monitor) throws IOException {

0 commit comments

Comments
 (0)