52 lines
1.5 KiB
PowerShell
52 lines
1.5 KiB
PowerShell
function Add-ZabbixFirewallRules {
|
|
param (
|
|
[Parameter(Mandatory=$true)]
|
|
[string]$FilePath,
|
|
|
|
$RuleName = "Zabbix Agent 2"
|
|
)
|
|
|
|
begin {
|
|
# Ensure the required module is available
|
|
if (-not (Get-Module -Name NetSecurity)) {
|
|
Import-Module NetSecurity -ErrorAction Stop
|
|
}
|
|
|
|
$splat = @{
|
|
DisplayName = $ruleName
|
|
Program = $FilePath
|
|
Action = 'Allow'
|
|
ErrorAction = 'Stop'
|
|
}
|
|
|
|
$directionList = 'Outbound', 'Inbound'
|
|
}
|
|
|
|
process {
|
|
try {
|
|
|
|
foreach ($direction in $directionList) {
|
|
|
|
# Check if the rule already exists
|
|
$existingRule = Get-NetFirewallRule -DisplayName $ruleName -ErrorAction SilentlyContinue | ? {$_.Direction -eq $direction}
|
|
|
|
if ($null -eq $existingRule) {
|
|
Write-Verbose "Creating new firewall rule..."
|
|
|
|
New-NetFirewallRule @splat -Direction $direction
|
|
|
|
Write-Verbose "Firewall rule created successfully."
|
|
} else {
|
|
Write-Verbose "Firewall rule already exists. No changes made."
|
|
}
|
|
}
|
|
} catch {
|
|
Write-Error "An error occurred while setting the firewall rule: $_"
|
|
throw
|
|
}
|
|
}
|
|
|
|
end {
|
|
# Optionally, you can add any cleanup or finalization logic here if needed.
|
|
}
|
|
}
|