Skip to content

Commit a768a39

Browse files
committed
Ensure 'WinDefend' service is running before querying MpComputerStatus
Fixes #1696
1 parent 41d170e commit a768a39

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WindowsDefenderConfigurator.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.concurrent.Future;
3131
import java.util.stream.Collectors;
3232
import org.eclipse.core.runtime.CoreException;
33+
import org.eclipse.core.runtime.ILog;
3334
import org.eclipse.core.runtime.IProduct;
3435
import org.eclipse.core.runtime.IProgressMonitor;
3536
import org.eclipse.core.runtime.OperationCanceledException;
@@ -149,8 +150,8 @@ private enum HandlingOption {
149150
* Windows Defender is inactive and null if the process was aborted.
150151
*/
151152
private static Boolean runExclusionCheck(IProgressMonitor m, Optional<Path> installLocation) throws CoreException {
152-
SubMonitor monitor = SubMonitor.convert(m, 3);
153-
if (!isWindowsDefenderActive(monitor.split(1))) {
153+
SubMonitor monitor = SubMonitor.convert(m, 4);
154+
if (!isWindowsDefenderServiceRunning(monitor.split(1)) || !isWindowsDefenderActive(monitor.split(1))) {
154155
return Boolean.FALSE;
155156
}
156157
Display display = Display.getDefault();
@@ -275,6 +276,19 @@ private static List<Path> getExecutablePath() {
275276
return List.of(Path.of(eclipseLauncher));
276277
}
277278

279+
private static boolean isWindowsDefenderServiceRunning(IProgressMonitor monitor) {
280+
// https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-service?view=powershell-7.4
281+
// https://learn.microsoft.com/en-us/dotnet/api/system.serviceprocess.servicecontrollerstatus?view=dotnet-plat-ext-8.0
282+
try {
283+
List<String> result = runProcess(List.of("powershell.exe", "-Command", "(Get-Service 'WinDefend').Status"), //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
284+
monitor);
285+
return result.size() == 1 && "Running".equalsIgnoreCase(result.get(0)); //$NON-NLS-1$
286+
} catch (IOException e) {
287+
ILog.get().error("Failed to obtain 'WinDefend' service state", e); //$NON-NLS-1$
288+
return false;
289+
}
290+
}
291+
278292
private static boolean isWindowsDefenderActive(IProgressMonitor monitor) throws CoreException {
279293
// https://learn.microsoft.com/en-us/powershell/module/defender/get-mpcomputerstatus
280294
List<String> command = List.of("powershell.exe", "-Command", "(Get-MpComputerStatus).AMRunningMode"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$

0 commit comments

Comments
 (0)