-
-
Notifications
You must be signed in to change notification settings - Fork 419
/
add-firewall-rules.ps1
executable file
·54 lines (45 loc) · 1.64 KB
/
add-firewall-rules.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
<#
.SYNOPSIS
Adds firewall rules for executables (needs admin rights).
.DESCRIPTION
This PowerShell script adds firewall rules for the given executable. Administrator rights are required.
.PARAMETER PathToExecutables
Specifies the path to the executables.
.PARAMETER Direction
Specifies the direction for the firewall rule. Can be 'Inbound' or 'Outbound'. Default is 'Inbound'.
.PARAMETER Profile
Specifies the firewall profile. Can be 'Domain', 'Private', or 'Public'. Multiple values can be specified as an array.
.EXAMPLE
PS> ./add-firewall-rules.ps1 -PathToExecutables C:\MyApp\bin -Direction Outbound -Profile Private
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
#Requires -RunAsAdministrator
param(
[string]$PathToExecutables = "",
[string]$Direction = "Inbound",
[array]$FirewallProfile = @("Domain", "Private")
)
try {
if (-not $PathToExecutables) {
$PathToExecutables = Read-Host "Enter path to executables"
}
$AbsPath = Convert-Path -Path $PathToExecutables
$Executables = Get-ChildItem -Path $AbsPath -Filter "*.exe"
if (-not $Executables) {
Write-Warning "No executables found. No Firewall rules have been created."
Read-Host "Press Enter to continue..."
return
}
foreach ($exe in $Executables) {
$exeName = $exe.Name
$exeFullPath = $exe.FullName
Write-Output "Adding firewall rule for $exeName"
New-NetFirewallRule -DisplayName $exeName -Direction $Direction -Program $exeFullPath -Profile $FirewallProfile -Action Allow
}
Write-Host -ForegroundColor Green "Done"
} catch {
Write-Error "Error in line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)"
}