-
Notifications
You must be signed in to change notification settings - Fork 220
Optimize Windows Defender exclusion script with native PowerShell #2930 #2939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -342,14 +342,12 @@ 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("'", "''") + "'") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please explain the advantage of using single-quotes (and escaping them) over double-quotes in a powershell script? As far as I can tell double-quotes are not allowed in Windows paths and therefore don't have to be escaped. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had initially done this to handle the rare case of paths containing single quotes (which are technically valid in Windows paths) |
||
.collect(Collectors.joining(',')); | ||
IamLRBA marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return String.join(";", | ||
"$pathsToExclude = @(" + excludedPaths + ")", | ||
"Add-MpPreference -ExclusionProcess $pathsToExclude" | ||
IamLRBA marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
} | ||
|
||
private static void excludeDirectoryFromScanning(IProgressMonitor monitor) throws IOException { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This entire comment block (starting at its first line) can be replaced by the following: