|
| 1 | +function Use-DefenderExclusionTool { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Downloads, extracts, and runs the Defender Exclusion Tool to manage exclusions in Microsoft Defender Antivirus. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + The Defender Exclusion Tool allows users to add or remove files, folders, file types, or processes from the Microsoft Defender Antivirus exclusion list. |
| 8 | +
|
| 9 | + .EXAMPLE |
| 10 | + Use-DefenderExclusionTool -StartApplication |
| 11 | +
|
| 12 | + .NOTES |
| 13 | + v0.0.1 |
| 14 | + #> |
| 15 | + [CmdletBinding()] |
| 16 | + param ( |
| 17 | + [Parameter(Mandatory = $false, HelpMessage = "Command-line arguments for the Defender Exclusion Tool")] |
| 18 | + [string]$CommandLineArgs, |
| 19 | + |
| 20 | + [Parameter(Mandatory = $false, HelpMessage = "URL for downloading Defender Exclusion Tool")] |
| 21 | + [uri]$DownloadUrl = "https://www.sordum.org/files/download/defender-exclusion-tool/ExcTool.zip", |
| 22 | + |
| 23 | + [Parameter(Mandatory = $false, HelpMessage = "Path to the directory where Defender Exclusion Tool will be downloaded and extracted")] |
| 24 | + [string]$DownloadPath = "$env:TEMP\DefenderExclusionTool", |
| 25 | + |
| 26 | + [Parameter(Mandatory = $false, HelpMessage = "Remove the temporary folder after the operation")] |
| 27 | + [switch]$RemoveFiles, |
| 28 | + |
| 29 | + [Parameter(Mandatory = $false, HelpMessage = "Start the Defender Exclusion Tool application after extraction")] |
| 30 | + [switch]$StartApplication |
| 31 | + ) |
| 32 | + $ZipFilePath = Join-Path $DownloadPath "ExcTool.zip" |
| 33 | + $ExtractPath = Join-Path $DownloadPath "ExcTool_v1.3" |
| 34 | + $Executable = Join-Path $ExtractPath "ExcTool.exe" |
| 35 | + try { |
| 36 | + Write-Host "Creating download directory..." -ForegroundColor Green |
| 37 | + if (-not (Test-Path -Path $DownloadPath)) { |
| 38 | + New-Item -Path $DownloadPath -ItemType Directory -Force | Out-Null |
| 39 | + } |
| 40 | + if (-not (Test-Path -Path $ZipFilePath)) { |
| 41 | + Write-Host "Downloading Defender Exclusion Tool..." -ForegroundColor Green |
| 42 | + Invoke-WebRequest -Uri $DownloadUrl -OutFile $ZipFilePath -UseBasicParsing -Verbose |
| 43 | + if ((Get-Item $ZipFilePath).Length -eq 0) { |
| 44 | + throw "The downloaded ZIP file is empty or corrupt." |
| 45 | + } |
| 46 | + } |
| 47 | + Write-Host "Extracting Defender Exclusion Tool..." -ForegroundColor Green |
| 48 | + if (Test-Path -Path $ExtractPath) { |
| 49 | + Remove-Item -Path $ExtractPath -Recurse -Force -ErrorAction Stop |
| 50 | + } |
| 51 | + try { |
| 52 | + Add-Type -AssemblyName System.IO.Compression.FileSystem |
| 53 | + [System.IO.Compression.ZipFile]::ExtractToDirectory($ZipFilePath, $DownloadPath) |
| 54 | + } |
| 55 | + catch { |
| 56 | + Write-Host "Extracting with Shell.Application..." -ForegroundColor Yellow |
| 57 | + $Shell = New-Object -ComObject Shell.Application |
| 58 | + $Zip = $Shell.NameSpace($ZipFilePath) |
| 59 | + $Destination = $Shell.NameSpace($DownloadPath) |
| 60 | + $Destination.CopyHere($Zip.Items(), 4) |
| 61 | + } |
| 62 | + Write-Host "Files in extraction directory:" -ForegroundColor Yellow |
| 63 | + Get-ChildItem -Path $DownloadPath -Recurse | ForEach-Object { |
| 64 | + Write-Host $_.FullName -ForegroundColor Yellow |
| 65 | + } |
| 66 | + if (-Not (Test-Path -Path $Executable)) { |
| 67 | + throw "Defender Exclusion Tool executable not found in $ExtractPath" |
| 68 | + } |
| 69 | + Write-Verbose -Message "Defender Exclusion Tool executable located at: $($Executable)" |
| 70 | + if ($StartApplication) { |
| 71 | + Write-Host "Starting Defender Exclusion Tool..." -ForegroundColor Green |
| 72 | + if ($CommandLineArgs) { |
| 73 | + Start-Process -FilePath $Executable -ArgumentList $CommandLineArgs |
| 74 | + } |
| 75 | + else { |
| 76 | + Start-Process -FilePath $Executable |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + catch { |
| 81 | + Write-Error -Message "An error occurred: $_" |
| 82 | + } |
| 83 | + finally { |
| 84 | + Write-Host "Defender Exclusion Tool operation completed." -ForegroundColor Cyan |
| 85 | + if ($RemoveFiles) { |
| 86 | + Write-Warning -Message "Cleaning up, removing the temporary folder..." |
| 87 | + Remove-Item -Path $DownloadPath -Force -Recurse -Verbose |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments