2023-07-29 10:34:04 +02:00
|
|
|
*add-firewall-rules.ps1*
|
|
|
|
================
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2022-02-10 09:01:07 +01:00
|
|
|
This PowerShell script adds firewall rules for the given executable. Administrator rights are required.
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
2023-09-13 09:49:05 +02:00
|
|
|
PS> ./add-firewall-rules.ps1 [[-PathToExecutables] <String>] [[-Direction] <String>] [[-FirewallProfile] <Array>] [<CommonParameters>]
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
-PathToExecutables <String>
|
2023-09-13 09:49:05 +02:00
|
|
|
Specifies the path to the executables.
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 1
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
2023-09-13 09:49:05 +02:00
|
|
|
-Direction <String>
|
|
|
|
Specifies the direction for the firewall rule. Can be 'Inbound' or 'Outbound'. Default is 'Inbound'.
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 2
|
|
|
|
Default value Inbound
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
-FirewallProfile <Array>
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 3
|
|
|
|
Default value @("Domain", "Private")
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
2021-11-08 21:36:42 +01:00
|
|
|
[<CommonParameters>]
|
|
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Example
|
|
|
|
-------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
2023-09-13 09:49:05 +02:00
|
|
|
PS> ./add-firewall-rules.ps1 -PathToExecutables C:\MyApp\bin -Direction Outbound -Profile Private
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Notes
|
|
|
|
-----
|
2022-11-17 19:46:02 +01:00
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Related Links
|
|
|
|
-------------
|
2021-11-08 21:36:42 +01:00
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Script Content
|
|
|
|
--------------
|
2022-11-17 20:05:34 +01:00
|
|
|
```powershell
|
2022-11-17 20:02:26 +01:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
2023-09-13 09:49:05 +02:00
|
|
|
Adds firewall rules for executables (needs admin rights).
|
2022-11-17 20:02:26 +01:00
|
|
|
.DESCRIPTION
|
|
|
|
This PowerShell script adds firewall rules for the given executable. Administrator rights are required.
|
|
|
|
.PARAMETER PathToExecutables
|
2023-09-13 09:49:05 +02:00
|
|
|
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.
|
2022-11-17 20:02:26 +01:00
|
|
|
.EXAMPLE
|
2023-09-13 09:49:05 +02:00
|
|
|
PS> ./add-firewall-rules.ps1 -PathToExecutables C:\MyApp\bin -Direction Outbound -Profile Private
|
2022-11-17 20:02:26 +01:00
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
|
|
|
#Requires -RunAsAdministrator
|
|
|
|
|
2023-09-13 09:49:05 +02:00
|
|
|
param(
|
|
|
|
[string]$PathToExecutables = "",
|
|
|
|
[string]$Direction = "Inbound",
|
|
|
|
[array]$FirewallProfile = @("Domain", "Private")
|
|
|
|
)
|
2022-11-17 20:02:26 +01:00
|
|
|
|
|
|
|
try {
|
2023-09-13 09:49:05 +02:00
|
|
|
if (-not $PathToExecutables) {
|
|
|
|
$PathToExecutables = Read-Host "Enter path to executables"
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
|
2023-09-13 09:49:05 +02:00
|
|
|
$AbsPath = Convert-Path -Path $PathToExecutables
|
|
|
|
$Executables = Get-ChildItem -Path $AbsPath -Filter "*.exe"
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2023-09-13 09:49:05 +02:00
|
|
|
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
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2023-09-13 09:49:05 +02:00
|
|
|
Write-Output "Adding firewall rule for $exeName"
|
|
|
|
New-NetFirewallRule -DisplayName $exeName -Direction $Direction -Program $exeFullPath -Profile $FirewallProfile -Action Allow
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
|
2023-09-13 09:49:05 +02:00
|
|
|
Write-Host -ForegroundColor Green "Done"
|
2022-11-17 20:02:26 +01:00
|
|
|
} catch {
|
2023-09-13 09:49:05 +02:00
|
|
|
Write-Error "Error in line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)"
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
2023-09-13 09:49:05 +02:00
|
|
|
|
2022-11-17 20:05:34 +01:00
|
|
|
```
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2023-12-16 10:12:58 +01:00
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of add-firewall-rules.ps1 as of 12/16/2023 10:12:17)*
|