windows - 通过 PowerShell 添加 Windows 防火墙规则
问题描述
我通过从 3 个数组中获取对象并填充 $Params 以发出 New-NetFirewallRule 命令,在 PowerShell 上添加 Windows 防火墙规则。我无法弄清楚为什么我的第一个命令失败并出现错误“端口号不正确”
代码:
$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时,添加的规则没有错误。
任何人都可以帮助我,因为它已经让我发疯了两天。
提前致谢!
解决方案
-LocalPort
的参数New-NetFirewallRule
被声明为一个数组String[]
。所以当你想传递多个端口时,你必须创建一个嵌套数组:
$AllPorts = @( @('8883', '443'), '443', '80' )
推荐阅读
- r - 如何在 R 语言的 sum() 中添加函数
- dns - 寻找快速缓存 DNS 解析器(服务器或库)
- elasticsearch - ElasticSearch(cURL REST API 请求)未给出预期结果
- elasticsearch - ElasticSearch / Kibana 按名称聚合计数
- oracle - Oracle 在用户使用相同密码时锁定用户
- java - 如何为 RecyclerView 设置 BaseAdapter?
- jhipster - Jhipster 在将日志发送到 logstash 时添加新文件
- python - 在文本文件中查找大于 x 的数字数量
- r - 如何在R中将一列分成两列?
- python - 获取TypeError:一元+的错误操作数类型:尝试使用子进程时的'str'