mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-02-18 02:30:52 +01:00
Refactor and Enhance Firewall Script
- Replaced `$Profile` with `$FirewallProfile` to avoid conflict with built-in PowerShell variable. - Introduced parameters for firewall rule direction (`$Direction`) and profile (`$FirewallProfile`). - Removed redundant elevation using `Start-Process` and retained `#Requires -RunAsAdministrator`. - Simplified path handling using `Convert-Path` and `-Filter` parameter of `Get-ChildItem`. - Streamlined the loop for adding firewall rules with a direct `foreach`. - Enhanced error handling for better user feedback and interaction. This refactor improves readability, flexibility, and reduces potential for errors in the script.
This commit is contained in:
parent
84a62b5d4c
commit
b9deb748b0
@ -1,15 +1,16 @@
|
|||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Adds firewall rules for executables (needs admin rights)
|
Adds firewall rules for executables (needs admin rights).
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
This PowerShell script adds firewall rules for the given executable. Administrator rights are required.
|
This PowerShell script adds firewall rules for the given executable. Administrator rights are required.
|
||||||
.PARAMETER PathToExecutables
|
.PARAMETER PathToExecutables
|
||||||
Specifies the path to the executables
|
Specifies the path to the executables.
|
||||||
|
.PARAMETER Direction
|
||||||
|
Specifies the direction for the firewall rule. Can be 'Inbound' or 'Outbound'. Default is 'Inbound'.
|
||||||
|
.PARAMETER FirewallProfile
|
||||||
|
Specifies the firewall profile. Can be 'Domain', 'Private', or 'Public'. Multiple values can be specified as an array.
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
PS> ./add-firewall-rules.ps1 C:\MyApp\bin
|
PS> ./add-firewall-rules.ps1 -PathToExecutables C:\MyApp\bin -Direction Outbound -Profile Private
|
||||||
Adding firewall rule for C:\MyApp\bin\app1.exe
|
|
||||||
Adding firewall rule for C:\MyApp\bin\app2.exe
|
|
||||||
...
|
|
||||||
.LINK
|
.LINK
|
||||||
https://github.com/fleschutz/PowerShell
|
https://github.com/fleschutz/PowerShell
|
||||||
.NOTES
|
.NOTES
|
||||||
@ -18,47 +19,36 @@
|
|||||||
|
|
||||||
#Requires -RunAsAdministrator
|
#Requires -RunAsAdministrator
|
||||||
|
|
||||||
param([string]$PathToExecutables = "")
|
param(
|
||||||
|
[string]$PathToExecutables = "",
|
||||||
$command = '
|
[string]$Direction = "Inbound",
|
||||||
$output = ''Firewall rules for path '' + $args[0]
|
[array]$FirewallProfile = @("Domain", "Private")
|
||||||
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 {
|
try {
|
||||||
if ($PathToExecutables -eq "" ) {
|
if (-not $PathToExecutables) {
|
||||||
$PathToExecutables = read-host "Enter path to executables"
|
$PathToExecutables = Read-Host "Enter path to executables"
|
||||||
}
|
}
|
||||||
|
|
||||||
$PathToExecutables = Convert-Path -Path $PathToExecutables
|
$AbsPath = Convert-Path -Path $PathToExecutables
|
||||||
|
$Executables = Get-ChildItem -Path $AbsPath -Filter "*.exe"
|
||||||
|
|
||||||
$Apps = Get-ChildItem "$PathToExecutables\*.exe" -Name
|
if (-not $Executables) {
|
||||||
|
Write-Warning "No executables found. No Firewall rules have been created."
|
||||||
if($Apps.count -eq 0){
|
Read-Host "Press Enter to continue..."
|
||||||
write-warning "No executables found. No Firewall rules have been created."
|
return
|
||||||
Write-Host -NoNewhLine 'Press any key to continue...';
|
|
||||||
[void]$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
|
|
||||||
exit 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$arg = "PathToExecutables $Apps"
|
foreach ($exe in $Executables) {
|
||||||
Start-Process powershell -Verb runAs -ArgumentList "-command & {$command} $arg"
|
$exeName = $exe.Name
|
||||||
exit 0 # success
|
$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 {
|
} catch {
|
||||||
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
Write-Error "Error in line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)"
|
||||||
exit 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user