Skip to content

Commit 9617dcf

Browse files
committed
i Use-FirewallAppBlocker
See SYNOPSIS/DESCRIPTION
1 parent e2c6343 commit 9617dcf

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
function Use-FirewallAppBlocker {
2+
<#
3+
.SYNOPSIS
4+
Manages firewall rules using the Firewall App Blocker tool.
5+
6+
.DESCRIPTION
7+
This function downloads and executes Firewall App Blocker to manage firewall rules, including adding, deleting, enabling, or disabling rules. Credits to Sordum.
8+
9+
.EXAMPLE
10+
Use-FirewallAppBlocker -Command '/A' -FilePath 'C:\ExampleApp.exe'
11+
Use-FirewallAppBlocker -Command '/D /In' -FilePath 'C:\ExampleApp.exe'
12+
Use-FirewallAppBlocker -Command '/ER' -FilePath 'TargetRuleName' -StartApplication
13+
Use-FirewallAppBlocker -Command '/O 1'
14+
15+
.NOTES
16+
v0.0.1
17+
#>
18+
[CmdletBinding()]
19+
param (
20+
[Parameter(Mandatory = $true, HelpMessage = "Specify the command to execute with Firewall App Blocker")]
21+
[ValidateSet('/A', '/D', '/Out', '/In', '/Block', '/Allow', '/ER', '/DR', '/I', '/O')]
22+
[string]$Command,
23+
24+
[Parameter(Mandatory = $false, HelpMessage = "File path or folder path for the rule")]
25+
[string]$FilePath = "",
26+
27+
[Parameter(Mandatory = $false, HelpMessage = "URL for downloading the Firewall App Blocker tool")]
28+
[uri]$FabDownloadUrl = "https://www.sordum.org/files/download/firewall-app-blocker/fab.zip",
29+
30+
[Parameter(Mandatory = $false, HelpMessage = "Path to the directory where the Firewall App Blocker tool will be downloaded and extracted")]
31+
[string]$DownloadPath = "$env:TEMP\FirewallAppBlocker",
32+
33+
[Parameter(Mandatory = $false, HelpMessage = "Remove the temporary folder after the operation")]
34+
[switch]$RemoveFirewallAppBlocker,
35+
36+
[Parameter(Mandatory = $false, HelpMessage = "Start the Firewall App Blocker application after extraction")]
37+
[switch]$StartApplication
38+
)
39+
if ($Command -in @('/A', '/D', '/ER', '/DR', '/I') -and -not $FilePath) {
40+
throw "FilePath must be provided when Command is '/A', '/D', '/ER', '/DR', or '/I'."
41+
}
42+
$FabZipPath = Join-Path $DownloadPath "fab.zip"
43+
$FabExtractPath = Join-Path $DownloadPath "FirewallAppBlocker"
44+
try {
45+
Write-Host "Creating download directory..." -ForegroundColor Green
46+
New-Item -Path $DownloadPath -ItemType Directory -Force | Out-Null
47+
if (!(Test-Path -Path $FabZipPath)) {
48+
Write-Host "Downloading Firewall App Blocker tool..." -ForegroundColor Green
49+
Invoke-WebRequest -Uri $FabDownloadUrl -OutFile $FabZipPath -UseBasicParsing -Verbose
50+
if ((Get-Item $FabZipPath).Length -eq 0) {
51+
throw "The downloaded ZIP file is empty or corrupt."
52+
}
53+
}
54+
Write-Host "Extracting Firewall App Blocker tool..." -ForegroundColor Green
55+
if (Test-Path -Path $FabExtractPath) {
56+
Remove-Item -Path $FabExtractPath -Recurse -Force
57+
}
58+
try {
59+
[System.IO.Compression.ZipFile]::ExtractToDirectory($FabZipPath, $FabExtractPath)
60+
}
61+
catch {
62+
throw "Failed to extract the ZIP file. It may be corrupt or incomplete."
63+
}
64+
$FabExecutable = Get-ChildItem -Path $FabExtractPath -Recurse -Filter "Fab_x64.exe" | Select-Object -First 1
65+
if (-Not $FabExecutable) {
66+
throw "Fab_x64.exe not found in $FabExtractPath"
67+
}
68+
$Arguments = $Command
69+
if ($FilePath) {
70+
$Arguments += " $FilePath"
71+
}
72+
Write-Verbose -Message "Starting Firewall App Blocker with arguments: $Arguments"
73+
if ($StartApplication) {
74+
Start-Process -FilePath $FabExecutable.FullName
75+
}
76+
else {
77+
Start-Process -FilePath $FabExecutable.FullName -ArgumentList $Arguments -WindowStyle Hidden -Wait
78+
}
79+
}
80+
catch {
81+
Write-Error -Message "An error occurred: $_"
82+
}
83+
finally {
84+
Write-Host "Firewall operation '$Command' completed." -ForegroundColor Cyan
85+
if ($RemoveFirewallAppBlocker) {
86+
Write-Warning -Message "Cleaning up, removing the temporary folder..."
87+
Remove-Item -Path $DownloadPath -Force -Recurse -Verbose
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)