2021-08-26 10:46:12 +02:00
|
|
|
|
<#
|
2021-07-13 19:03:30 +02:00
|
|
|
|
.SYNOPSIS
|
|
|
|
|
add-firewall-rules.ps1 [<path-to-executables>]
|
|
|
|
|
.DESCRIPTION
|
2021-08-29 17:50:03 +02:00
|
|
|
|
Adds firewall rules for the given executables (needs administrator rights).
|
2021-07-13 19:03:30 +02:00
|
|
|
|
.EXAMPLE
|
|
|
|
|
PS> .\add-firewall-rules.ps1 C:\MyApp\bin
|
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
.NOTES
|
2021-08-29 17:50:03 +02:00
|
|
|
|
Author: Markus Fleschutz · License: CC0
|
2021-01-28 15:02:16 +01:00
|
|
|
|
#>
|
|
|
|
|
|
|
|
|
|
#Requires -RunAsAdministrator
|
|
|
|
|
|
2021-02-01 08:25:03 +01:00
|
|
|
|
param([string]$PathToExecutables = "")
|
2021-01-28 15:02:16 +01:00
|
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
}
|
2021-02-10 19:25:48 +01:00
|
|
|
|
write-host -foregroundColor green -noNewline ''Done - press any key to continue...'';
|
2021-02-01 08:25:03 +01:00
|
|
|
|
[void]$Host.UI.RawUI.ReadKey(''NoEcho,IncludeKeyDown'');
|
2021-01-28 15:02:16 +01:00
|
|
|
|
'
|
|
|
|
|
|
2021-02-01 08:25:03 +01:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if ($PathToExecutables -eq "" ) {
|
|
|
|
|
$PathToExecutables = read-host "Enter path to executables"
|
2021-01-28 15:02:16 +01:00
|
|
|
|
}
|
2021-02-01 08:25:03 +01:00
|
|
|
|
|
|
|
|
|
$PathToExecutables = Convert-Path -Path $PathToExecutables
|
|
|
|
|
|
|
|
|
|
$Apps = Get-ChildItem "$PathToExecutables\*.exe" -Name
|
2021-01-28 15:02:16 +01:00
|
|
|
|
|
|
|
|
|
if($Apps.count -eq 0){
|
2021-02-01 08:25:03 +01:00
|
|
|
|
write-warning "No executables found. No Firewall rules have been created."
|
2021-01-28 15:02:16 +01:00
|
|
|
|
Write-Host -NoNewhLine 'Press any key to continue...';
|
2021-02-01 08:25:03 +01:00
|
|
|
|
[void]$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
|
|
|
|
|
exit 1
|
2021-01-28 15:02:16 +01:00
|
|
|
|
}
|
2021-02-01 08:25:03 +01:00
|
|
|
|
|
|
|
|
|
$arg = "PathToExecutables $Apps"
|
|
|
|
|
Start-Process powershell -Verb runAs -ArgumentList "-command & {$command} $arg"
|
|
|
|
|
exit 0
|
|
|
|
|
} catch {
|
2021-05-02 21:30:48 +02:00
|
|
|
|
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
2021-02-01 08:25:03 +01:00
|
|
|
|
exit 1
|
2021-01-28 15:02:16 +01:00
|
|
|
|
}
|