# win_firewall_remove_rule.ps1 - Removes a Windows Firewall rule by name.
# Requires: Administrator privileges
# Usage: powershell.exe -ExecutionPolicy Bypass -File win_firewall_remove_rule.ps1 -Name "CDP-9222"

param(
    [Parameter(Mandatory = $true)]
    [string]$Name
)

# Verify administrator privileges
$currentPrincipal = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Write-Error "ERROR: This script requires Administrator privileges. Run PowerShell as Administrator."
    exit 1
}

# Check if the rule exists
$existingRule = netsh advfirewall firewall show rule name="$Name" 2>&1
if ($LASTEXITCODE -ne 0) {
    Write-Host "INFO: Firewall rule '$Name' does not exist - nothing to remove."
    exit 0
}

# Remove the rule
Write-Host "Removing firewall rule '$Name'..."
netsh advfirewall firewall delete rule name="$Name"

if ($LASTEXITCODE -ne 0) {
    Write-Error "ERROR: Failed to remove firewall rule '$Name'."
    exit 1
}

Write-Host "OK: Firewall rule '$Name' removed."
