From caf59c0e43574303d6a24d1ad7cce05212f7846d Mon Sep 17 00:00:00 2001 From: Jerry Laruba Festus Date: Fri, 25 Apr 2025 01:12:03 +0300 Subject: [PATCH 1/2] 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 https://github.com/eclipse-platform/eclipse.platform.ui/issues/2930 --- .../ui/internal/WindowsDefenderConfigurator.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WindowsDefenderConfigurator.java b/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WindowsDefenderConfigurator.java index bc105459999..b9504d13be9 100644 --- a/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WindowsDefenderConfigurator.java +++ b/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WindowsDefenderConfigurator.java @@ -342,14 +342,14 @@ public static String createAddExclusionsPowershellCommand(String extraSeparator) // // For .NET's stream API called LINQ see: // https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable - String excludedPaths = paths.stream().map(Path::toString).map(p -> '"' + p + '"') - .collect(Collectors.joining(',' + extraSeparator)); - final String exclusionType = "ExclusionProcess"; //$NON-NLS-1$ - return String.join(';' + extraSeparator, "$exclusions=@(" + extraSeparator + excludedPaths + ')', //$NON-NLS-1$ - "$existingExclusions=[Collections.Generic.HashSet[String]](Get-MpPreference)." + exclusionType, //$NON-NLS-1$ - "if($existingExclusions -eq $null) { $existingExclusions = New-Object Collections.Generic.HashSet[String] }", //$NON-NLS-1$ - "$exclusionsToAdd=[Linq.Enumerable]::ToArray([Linq.Enumerable]::Where($exclusions,[Func[object,bool]]{param($ex)!$existingExclusions.Contains($ex)}))", //$NON-NLS-1$ - "if($exclusionsToAdd.Length -gt 0){ Add-MpPreference -" + exclusionType + " $exclusionsToAdd }"); //$NON-NLS-1$ //$NON-NLS-2$ + String excludedPaths = paths.stream().map(Path::toString).map(p -> "'" + p.replace("'", "''") + "'") + .collect(Collectors.joining(',')); + return String.join(";", + "$pathsToExclude = @(" + excludedPaths + ")", + "$existingExclusions = New-Object Collections.Generic.HashSet[String] -ArgumentList @([string[]](Get-MpPreference).ExclusionProcess)", + "$pathsToAdd = $pathsToExclude | Where-Object { -not $existingExclusions.Contains($_) }", + "if ($pathsToAdd) { Add-MpPreference -ExclusionProcess $pathsToAdd }" + ); } private static void excludeDirectoryFromScanning(IProgressMonitor monitor) throws IOException { From 70f15950eec314b8931993cd3a230a2a41b719c6 Mon Sep 17 00:00:00 2001 From: Jerry Laruba Festus Date: Wed, 30 Apr 2025 14:53:52 +0300 Subject: [PATCH 2/2] 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 --- .../org/eclipse/ui/internal/WindowsDefenderConfigurator.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WindowsDefenderConfigurator.java b/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WindowsDefenderConfigurator.java index b9504d13be9..b22a1dc88ef 100644 --- a/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WindowsDefenderConfigurator.java +++ b/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WindowsDefenderConfigurator.java @@ -346,9 +346,7 @@ public static String createAddExclusionsPowershellCommand(String extraSeparator) .collect(Collectors.joining(',')); return String.join(";", "$pathsToExclude = @(" + excludedPaths + ")", - "$existingExclusions = New-Object Collections.Generic.HashSet[String] -ArgumentList @([string[]](Get-MpPreference).ExclusionProcess)", - "$pathsToAdd = $pathsToExclude | Where-Object { -not $existingExclusions.Contains($_) }", - "if ($pathsToAdd) { Add-MpPreference -ExclusionProcess $pathsToAdd }" + "Add-MpPreference -ExclusionProcess $pathsToExclude" ); }