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

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

我想知道如何使用 Power Shell 在 Windows 中打开防火墙端口。有人可以编写一个用于打开防火墙端口的脚本吗?我在https://stackoverflow.com/questions/24760821/changing-windows-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

答案1

您可以参考指南这里

打开80端口的命令是:

netsh advfirewall firewall add rule name="Open Port 80" dir=in action=allow protocol=TCP localport=80

您需要指定:

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

您可以从 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)

取自这里

答案2

根据以下信息这个博客

New-NetFirewallRule -DisplayName 'some-port' `
                    -LocalPort 1234 -Action Allow `
                    -Profile 'Public' `
                    -Protocol TCP `
                    -Direction Inbound

答案3

这是在防火墙上创建端口的另一种方法,其好处是系统将动态提示您输入与入站/出站、协议、允许/拒绝等相关的所有选项。


$rulename = Read-Host -Prompt "Enter rule name: "
$portNumber = Read-Host -Prompt "Enter Port Number: "
$protchoice = $Host.UI.PromptForChoice('Protocol Type','Enter Protocol (TCP/UDP): ',('&TCP','&UDP'),0)
if($protchoice -eq 0)
    {
        $protchoice02 = 'TCP'
        $dirChoice = $Host.UI.PromptForChoice('Traffic Flow','Enter Traffic Flow direction (inbound/outbound): ',('&inbound','&outbound'),0)
        if($dirChoice -eq 0)
        {
         $dirChoices = 'Inbound'
         $allowdenyChoice = $Host.UI.PromptForChoice('Traffic Allowance','Allow/Deny Traffic (allow/deny): ',('&allow','&deny'),0)
         if($allowdenyChoice -eq '0')
         { 
          $allowdenyChoices = 'Allow'
          New-NetFirewallRule -DisplayName $rulename -Direction $dirChoices -LocalPort $portNumber -Protocol $protchoice02 -Action $allowdenyChoices
         }
         else
         {
         $allowedenyChoices = 'Deny'
         New-NetFirewallRule -DisplayName $rulename -Direction $dirChoices  -LocalPort $portNumber -Protocol $protchoice02 -Action $allowdenyChoices
         }
        }
        else
        {
          $dirChoices = 'Outbound'
          $allowdenyChoice = $Host.UI.PromptForChoice('Traffic Allowance','Allow/Deny Traffic (allow/deny): ',('&allow','&deny'),0)
          if($allowdenyChoice -eq '0')
          { 
           $allowdenyChoices = 'Allow'
           New-NetFirewallRule -DisplayName $rulename -Direction $dirChoices -LocalPort $portNumber -Protocol $protchoice02 -Action $allowdenyChoices
          }
          else
          {
           $allowedenyChoices = 'Deny'
           New-NetFirewallRule -DisplayName $rulename -Direction $dirChoices  -LocalPort $portNumber -Protocol $protchoice02 -Action $allowdenyChoices
          }
         }
       }
else
    { 
        $protchoice02 = 'UDP'
        $dirChoice = $Host.UI.PromptForChoice('Traffic Flow','Enter Traffic Flow direction (inbound/outbound): ',('&inbound','&outbound'),0)
        if($dirChoice -eq 0)
        {
         $dirChoices = 'Inbound'
         $allowdenyChoice = $Host.UI.PromptForChoice('Traffic Allowance','Allow/Deny Traffic (allow/deny): ',('&allow','&deny'),0)
         if($allowdenyChoice -eq '0')
         { 
          $allowdenyChoices = 'Allow'
          New-NetFirewallRule -DisplayName $rulename -Direction $dirChoices -LocalPort $portNumber -Protocol $protchoice02 -Action $allowdenyChoices
         }
         else
         {
          $allowedenyChoices = 'Deny'
          New-NetFirewallRule -DisplayName $rulename -Direction $dirChoices -LocalPort $portNumber -Protocol $protchoice02 -Action $allowdenyChoices
         }
        }
        else
        {
          $dirChoices = 'Outbound'
          $allowdenyChoice = $Host.UI.PromptForChoice('Traffic Allowance','Allow/Deny Traffic (allow/deny): ',('&allow','&deny'),0)
          if($allowdenyChoice -eq '0')
          { 
           $allowdenyChoices = 'Allow'
           New-NetFirewallRule -DisplayName $rulename -Direction $dirChoices -LocalPort $portNumber -Protocol $protchoice02 -Action $allowdenyChoices
          }
          else
          {
           $allowedenyChoices = 'Deny'
           New-NetFirewallRule -DisplayName $rulename -Direction $dirChoices -LocalPort $portNumber -Protocol $protchoice02 -Action $allowdenyChoices
          }
         }
       }  

相关内容