如何使用 Power Shell 在 Windows 中打开防火墙端口

jus*_*tin 9 networking firewall powershell

我想知道如何使用 Power Shell 在 Windows 中打开防火墙端口。谁能写一个脚本来打开防火墙端口。我在/sf/ask/1733257501/上看到了类似的帖子-firewall-rules-with-powershell-open-close-a-specific-port但不知道怎么做。

我只想在 Windows 中打开一个端口:8983,因为当我执行应用程序(堆栈转储)时,它说pysolr.SolrError: Failed to connect to server at 'http://localhost:8983/solr/stackdump/admin/ping', are you sure that URL is correct?.Atlast 它说:No connection could be made because the target machine actively refused it

mnm*_*mnc 18

您可以参考此处的指南。

打开80端口的命令是:

netsh advfirewall firewall add rule name="Open Port 80" dir=in action=allow protocol=TCP localport=80
Run Code Online (Sandbox Code Playgroud)

您需要指定:

  • 规则名称
  • 方向
  • 是否允许连接
  • 使用的协议
  • 端口号

您可以从 Powershell 级别使用此命令。

如果您绝对必须使用 Powershell,您可以使用类似下面的脚本(也适用于端口 80):

#==============================================================
# Creates a rule to open an incomming port in the firewall.
#==============================================================

#$numberAsString = read-host "type an port number"
#$mynumber = [int]$numberAsString


$port1 = New-Object -ComObject HNetCfg.FWOpenPort

$port1.Port = 80

$port1.Name = 'MyTestPort' # name of Port

$port1.Enabled = $true

$fwMgr = New-Object -ComObject HNetCfg.FwMgr

$profiledomain=$fwMgr.LocalPolicy.GetProfileByType(0)

$profiledomain.GloballyOpenPorts.Add($port1)
Run Code Online (Sandbox Code Playgroud)

取自这里