答案1
脚本:
[int]$port = 3777
)
$ruleName = "Allow Port $port"
# Check if the rule already exists
$existingRule = Get-NetFirewallRule -DisplayName $ruleName -ErrorAction SilentlyContinue
if ($existingRule) {
Write-Host "Firewall rule '$ruleName' already exists."
} else {
# Create a new inbound rule for the specified port
New-NetFirewallRule -DisplayName $ruleName -Direction Inbound -Action Allow -Protocol TCP -LocalPort $port
Write-Host "Firewall rule '$ruleName' created."
}
然后从文件夹运行脚本:\scriptname.ps1 -port 5000