You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本数组Join结果无法被New-NetFirewallRule识别,手动输入正常

解决PowerShell添加多端口防火墙规则的问题

问题根源在于New-NetFirewallRule的-LocalPort参数接受数组类型输入,而非逗号分隔的字符串。你用$ports -join ","生成的是单个字符串"80,5000",会被当成一个无效的完整端口值;而手动输入80,5000时,PowerShell会自动将其解析为包含两个元素的数组@(80,5000),因此能正常执行。

正确写法

无需做字符串拼接,直接将端口数组传入参数即可:

$firewallRuleName='Test Rule'
$ports=@(80,5000)
New-NetFireWallRule -DisplayName $firewallRuleName -Direction Outbound -Action Allow -Protocol TCP -LocalPort $ports

补充场景处理

如果端口是从其他渠道获取的逗号分隔字符串(比如"80,5000"),可以先拆分为数组再传入:

$portString = "80,5000"
$ports = $portString -split ","
New-NetFireWallRule -DisplayName $firewallRuleName -Direction Outbound -Action Allow -Protocol TCP -LocalPort $ports

内容的提问来源于stack exchange,提问作者Sweavo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 23:20:40