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-07-29 10:15:44 +02:00
|
|
|
PS> ./add-firewall-rules.ps1 [[-PathToExecutables] <String>] [<CommonParameters>]
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
-PathToExecutables <String>
|
|
|
|
Specifies the path to the executables
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 1
|
|
|
|
Default value
|
|
|
|
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.
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Example
|
|
|
|
-------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
|
|
|
PS> ./add-firewall-rules C:\MyApp\bin
|
|
|
|
Adding firewall rule for C:\MyApp\bin\app1.exe
|
|
|
|
Adding firewall rule for C:\MyApp\bin\app2.exe
|
|
|
|
...
|
|
|
|
|
|
|
|
```
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
.EXAMPLE
|
|
|
|
PS> ./add-firewall-rules C:\MyApp\bin
|
|
|
|
Adding firewall rule for C:\MyApp\bin\app1.exe
|
|
|
|
Adding firewall rule for C:\MyApp\bin\app2.exe
|
|
|
|
...
|
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
|
|
|
#Requires -RunAsAdministrator
|
|
|
|
|
|
|
|
param([string]$PathToExecutables = "")
|
|
|
|
|
|
|
|
$command = '
|
|
|
|
$output = ''Firewall rules for path '' + $args[0]
|
|
|
|
write-output $output
|
|
|
|
for($i = 1; $i -lt $args.count; $i++){
|
|
|
|
$path = $args[0]
|
|
|
|
$path += ''\''
|
|
|
|
$path += $args[$i]
|
|
|
|
|
|
|
|
$null = $args[$i] -match ''[^\\]*\.exe$''
|
|
|
|
$name = $matches[0]
|
|
|
|
$output = ''Adding firewall rule for '' + $name
|
|
|
|
write-output $output
|
|
|
|
$null = New-NetFirewallRule -DisplayName $name -Direction Inbound -Program $path -Profile Domain, Private -Action Allow
|
|
|
|
}
|
|
|
|
write-host -foregroundColor green -noNewline ''Done - press any key to continue...'';
|
|
|
|
[void]$Host.UI.RawUI.ReadKey(''NoEcho,IncludeKeyDown'');
|
|
|
|
'
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($PathToExecutables -eq "" ) {
|
|
|
|
$PathToExecutables = read-host "Enter path to executables"
|
|
|
|
}
|
|
|
|
|
|
|
|
$PathToExecutables = Convert-Path -Path $PathToExecutables
|
|
|
|
|
|
|
|
$Apps = Get-ChildItem "$PathToExecutables\*.exe" -Name
|
|
|
|
|
|
|
|
if($Apps.count -eq 0){
|
|
|
|
write-warning "No executables found. No Firewall rules have been created."
|
|
|
|
Write-Host -NoNewhLine 'Press any key to continue...';
|
|
|
|
[void]$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
$arg = "PathToExecutables $Apps"
|
|
|
|
Start-Process powershell -Verb runAs -ArgumentList "-command & {$command} $arg"
|
|
|
|
exit 0 # success
|
|
|
|
} catch {
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
|
|
|
}
|
2022-11-17 20:05:34 +01:00
|
|
|
```
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2023-08-06 11:42:46 +02:00
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of add-firewall-rules.ps1 as of 08/06/2023 11:42:25)*
|