【发布时间】:2021-04-26 17:56:51
【问题描述】:
我正在通过 PowerShell 添加 Windows 防火墙规则,方法是从 3 个数组中获取对象并填充 $Params 以发出 New-NetFirewallRule 命令。我无法弄清楚为什么我的第一个命令失败并出现错误“端口号不正确”
代码:
$All = @( '13.79.172.43' , '13.69.228.5' , '1.1.1.1' )
$AllPorts = @( '8883,443' , '443', '80' )
$AllProtocols = @( 'TCP' , 'TCP', 'TCP' )
for ($i = 0; $i -lt $All.Count; $i++) {
$Params = @{
"DisplayName" = '"Block-WiFi-' + $i
"Name" = 'Block-WiFi-' + $i
"Direction" = 'Inbound'
"InterfaceType" = 'Wireless'
"Action" = 'Block'
"RemoteAddress" = $All[$i]
"LocalPort" = $AllPorts[$i]
"Protocol" = $AllProtocols[$i]
}
# Add Windows Firewall RUle
New-NetFirewallRule @Params
# Check what is going on
Write-Host "Address: $($All[$i]) | Port: $($AllPorts[$i]) | Protocol: $($AllProtocols[$i])"
Write-Host "----------------------------------------------------------------------------------"
Start-Sleep 2
}
所以一切正常,除了尝试添加第一个 8883,443 对象时。
当我手动尝试命令时,它可以工作:
New-NetFirewallRule -DisplayName "Block-Wireless-In-01" -Name "Block-Wireless-In-01" -Direction Inbound -InterfaceType Wireless -Action Block -RemoteAddress 13.79.172.43 -LocalPort 8883,443 -Protocol TCP
此外,当我尝试添加 @Params "LocalPort" = 8883,443 时,添加的规则没有错误。
谁能帮帮我,因为这已经让我发疯了两天。
提前致谢!
【问题讨论】:
标签: windows powershell powershell-2.0 powershell-3.0 windows-firewall