mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-11-21 15:33:15 +01:00
117 lines
3.4 KiB
Markdown
117 lines
3.4 KiB
Markdown
The *add-firewall-rules.ps1* Script
|
|
===========================
|
|
|
|
This PowerShell script adds firewall rules for the given executable. Administrator rights are required.
|
|
|
|
Parameters
|
|
----------
|
|
```powershell
|
|
/home/markus/Repos/PowerShell/scripts/add-firewall-rules.ps1 [[-PathToExecutables] <String>] [[-Direction] <String>] [[-FirewallProfile] <Array>] [<CommonParameters>]
|
|
|
|
-PathToExecutables <String>
|
|
Specifies the path to the executables.
|
|
|
|
Required? false
|
|
Position? 1
|
|
Default value
|
|
Accept pipeline input? false
|
|
Accept wildcard characters? false
|
|
|
|
-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
|
|
|
|
[<CommonParameters>]
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
```
|
|
|
|
Example
|
|
-------
|
|
```powershell
|
|
PS> ./add-firewall-rules.ps1 -PathToExecutables C:\MyApp\bin -Direction Outbound -Profile Private
|
|
|
|
```
|
|
|
|
Notes
|
|
-----
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
Related Links
|
|
-------------
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
Script Content
|
|
--------------
|
|
```powershell
|
|
<#
|
|
.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)"
|
|
}
|
|
|
|
```
|
|
|
|
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:49)*
|