@@ -6,17 +6,34 @@ This PowerShell script adds firewall rules for the given executable. Administrat
6
6
Parameters
7
7
----------
8
8
``` powershell
9
- PS> ./add-firewall-rules.ps1 [[-PathToExecutables] <String>] [<CommonParameters>]
9
+ PS> ./add-firewall-rules.ps1 [[-PathToExecutables] <String>] [[-Direction] <String>] [[-FirewallProfile] <Array>] [ <CommonParameters>]
10
10
11
11
-PathToExecutables <String>
12
- Specifies the path to the executables
12
+ Specifies the path to the executables.
13
13
14
14
Required? false
15
15
Position? 1
16
16
Default value
17
17
Accept pipeline input? false
18
18
Accept wildcard characters? false
19
19
20
+ -Direction <String>
21
+ Specifies the direction for the firewall rule. Can be 'Inbound' or 'Outbound'. Default is 'Inbound'.
22
+
23
+ Required? false
24
+ Position? 2
25
+ Default value Inbound
26
+ Accept pipeline input? false
27
+ Accept wildcard characters? false
28
+
29
+ -FirewallProfile <Array>
30
+
31
+ Required? false
32
+ Position? 3
33
+ Default value @("Domain", "Private")
34
+ Accept pipeline input? false
35
+ Accept wildcard characters? false
36
+
20
37
[<CommonParameters>]
21
38
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
22
39
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
@@ -25,10 +42,7 @@ PS> ./add-firewall-rules.ps1 [[-PathToExecutables] <String>] [<CommonParameters>
25
42
Example
26
43
-------
27
44
``` powershell
28
- PS> ./add-firewall-rules.ps1 C:\MyApp\bin
29
- Adding firewall rule for C:\MyApp\bin\app1.exe
30
- Adding firewall rule for C:\MyApp\bin\app2.exe
31
- ...
45
+ PS> ./add-firewall-rules.ps1 -PathToExecutables C:\MyApp\bin -Direction Outbound -Profile Private
32
46
33
47
```
34
48
@@ -45,16 +59,17 @@ Script Content
45
59
``` powershell
46
60
<#
47
61
.SYNOPSIS
48
- Adds firewall rules for executables (needs admin rights)
62
+ Adds firewall rules for executables (needs admin rights).
49
63
.DESCRIPTION
50
64
This PowerShell script adds firewall rules for the given executable. Administrator rights are required.
51
65
.PARAMETER PathToExecutables
52
- Specifies the path to the executables
66
+ Specifies the path to the executables.
67
+ .PARAMETER Direction
68
+ Specifies the direction for the firewall rule. Can be 'Inbound' or 'Outbound'. Default is 'Inbound'.
69
+ .PARAMETER Profile
70
+ Specifies the firewall profile. Can be 'Domain', 'Private', or 'Public'. Multiple values can be specified as an array.
53
71
.EXAMPLE
54
- PS> ./add-firewall-rules.ps1 C:\MyApp\bin
55
- Adding firewall rule for C:\MyApp\bin\app1.exe
56
- Adding firewall rule for C:\MyApp\bin\app2.exe
57
- ...
72
+ PS> ./add-firewall-rules.ps1 -PathToExecutables C:\MyApp\bin -Direction Outbound -Profile Private
58
73
.LINK
59
74
https://github.com/fleschutz/PowerShell
60
75
.NOTES
@@ -63,50 +78,39 @@ Script Content
63
78
64
79
#Requires -RunAsAdministrator
65
80
66
- param([string]$PathToExecutables = "")
67
-
68
- $command = '
69
- $output = ''Firewall rules for path '' + $args[0]
70
- write-output $output
71
- for($i = 1; $i -lt $args.count; $i++){
72
- $path = $args[0]
73
- $path += ''\''
74
- $path += $args[$i]
75
-
76
- $null = $args[$i] -match ''[^\\]*\.exe$''
77
- $name = $matches[0]
78
- $output = ''Adding firewall rule for '' + $name
79
- write-output $output
80
- $null = New-NetFirewallRule -DisplayName $name -Direction Inbound -Program $path -Profile Domain, Private -Action Allow
81
- }
82
- write-host -foregroundColor green -noNewline ''Done - press any key to continue...'';
83
- [void]$Host.UI.RawUI.ReadKey(''NoEcho,IncludeKeyDown'');
84
- '
85
-
81
+ param(
82
+ [string]$PathToExecutables = "",
83
+ [string]$Direction = "Inbound",
84
+ [array]$FirewallProfile = @("Domain", "Private")
85
+ )
86
86
87
87
try {
88
- if ($PathToExecutables -eq "" ) {
89
- $PathToExecutables = read-host "Enter path to executables"
88
+ if (-not $PathToExecutables ) {
89
+ $PathToExecutables = Read-Host "Enter path to executables"
90
90
}
91
91
92
- $PathToExecutables = Convert-Path -Path $PathToExecutables
92
+ $AbsPath = Convert-Path -Path $PathToExecutables
93
+ $Executables = Get-ChildItem -Path $AbsPath -Filter "*.exe"
93
94
94
- $Apps = Get-ChildItem "$PathToExecutables\*.exe" -Name
95
+ if (-not $Executables) {
96
+ Write-Warning "No executables found. No Firewall rules have been created."
97
+ Read-Host "Press Enter to continue..."
98
+ return
99
+ }
100
+
101
+ foreach ($exe in $Executables) {
102
+ $exeName = $exe.Name
103
+ $exeFullPath = $exe.FullName
95
104
96
- if($Apps.count -eq 0){
97
- write-warning "No executables found. No Firewall rules have been created."
98
- Write-Host -NoNewhLine 'Press any key to continue...';
99
- [void]$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
100
- exit 1
105
+ Write-Output "Adding firewall rule for $exeName"
106
+ New-NetFirewallRule -DisplayName $exeName -Direction $Direction -Program $exeFullPath -Profile $FirewallProfile -Action Allow
101
107
}
102
108
103
- $arg = "PathToExecutables $Apps"
104
- Start-Process powershell -Verb runAs -ArgumentList "-command & {$command} $arg"
105
- exit 0 # success
109
+ Write-Host -ForegroundColor Green "Done"
106
110
} catch {
107
- "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
108
- exit 1
111
+ Write-Error "Error in line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)"
109
112
}
113
+
110
114
```
111
115
112
- * (generated by convert-ps2md.ps1 using the comment-based help of add-firewall-rules.ps1 as of 09/01 /2023 17:51:47 )*
116
+ * (generated by convert-ps2md.ps1 using the comment-based help of add-firewall-rules.ps1 as of 09/13 /2023 09:48:36 )*
0 commit comments