-
Notifications
You must be signed in to change notification settings - Fork 0
/
Block-OutboundConnectionsForAnApp.ps1
54 lines (45 loc) · 1.52 KB
/
Block-OutboundConnectionsForAnApp.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# block outbound connections for an .exe path
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, HelpMessage = "The path to the application")]
[String]$ApplicationPath
)
# Test if application exists
if (-not (Test-Path $ApplicationPath)) {
Write-Warning "'$ApplicationPath' does not exist"
return
}
$fileInfo = Get-Item $ApplicationPath
$appName = $fileInfo.Name
$productName = $fileInfo.VersionInfo.ProductName
$string = ""
if (-not [string]::IsNullOrEmpty($productName)) {
$string = $productName.Trim()
}
else {
$string = $fileInfo.BaseName.Trim()
}
if (-not [string]::IsNullOrEmpty($appName)) {
$string = $string + " - " + $appName
}
$ruleName = "[Custom] Block outbound access for $string ($applicationPath)"
# Test if the rule already exists
try {
$ruleExists = [boolean](Get-NetFirewallRule -Direction Outbound | Where-Object { $_.DisplayName -eq "$ruleName" })
}
catch {
Write-Warning "Failed to check if the rule already exists $($_.Exception.Message)."
}
if ($ruleExists) {
Write-Warning "The rule '$ruleName' already exists"
return
}
# create a new rule
try {
Write-Host -ForegroundColor Cyan "Creating a new rule to block outbound connections for the application: $ApplicationPath"
New-NetFirewallRule -DisplayName $ruleName -Direction Outbound -Program $ApplicationPath -Action Block -Enabled True -ErrorAction Stop
Write-Host -ForegroundColor Green "The rule has been created successfully."
}
catch {
Write-Host "Failed to create the rule. The rule may already exist."
}