使用PowerShell 2的Windows防火墙规则处理

时间:2019-06-07 15:27:03

标签: powershell firewall

只要安装的PowerShell版本为4+,我就有PowerShell代码来操纵Windows防火墙规则。但是我需要在使用PowerShell 2的Windows服务器上运行这些命令。我读过的所有内容都指向我使用Netshadvfirewall,但是我没有成功找到正确的方式来使用它来达到我的目的。

以下是我需要转换为PowerShell 2命令的5个PowerShell命令:

  • Get-NetFirewallRule
  • Get-NetFirewallAddressFilter
  • Get-NetFirewallPortFilter
  • Remove-NetFirewallRule
  • New-NetFirewallRule

适用于PowerShell 4+的当前代码

$RuleName = 'Test Rule Name'
$IPAddress = '1.1.1.1'
$Port = 127
$LocLocation = 'C:\temp\Firewall.log'

$FireWallRule = (Get-NetFirewallRule  -DisplayName "$RuleName" -ErrorAction SilentlyContinue)
if ($null -ne $FireWallRule) {
  $FirewallRuleIP = ($FirewallRule | Get-NetFirewallAddressFilter).RemoteAddress
  $FirewallRulePort = ($FirewallRule | Get-NetFirewallPortFilter).LocalPort

  # Is the existing firewall rule correctly configured?
  if ($FirewallRule.Direction -eq "Inbound" -and $FirewallRule.Action -eq "Allow" -and $FirewallRule.Enabled -eq "true" -and $FirewallRuleIP -eq $IPAddress -and $FirewallRulePort -eq $Port) {
    $Message = "Firewall rule $RuleName already exists and is configured correctly with:  Direction:Inbound, Action:Allow, Protocol:TCP, RemoteAddress:$IPAddress, LocalPort:$Port"
  }
  else {
    Remove-NetFirewallRule  -DisplayName "$RuleName" | Out-Null
    New-NetFirewallRule -DisplayName "$RuleName" -Direction Inbound -Action Allow -Protocol TCP -RemoteAddress $IPAddress -LocalPort $Port | Out-Null
    $Message = "Firewall rule $RuleName was misconfigured.  It was deleted and recreated with:  Direction:Inbound, Action:Allow, Protocol:TCP, RemoteAddress:$IPAddress, LocalPort:$Port"
  }
}

2 个答案:

答案 0 :(得分:1)

我认为这将适用于翻译:

$RuleName = 'Test Rule Name'
$IPAddress = '1.1.1.1'
$Port = 127
$LocLocation = 'C:\temp\Firewall.log'
$FireWallRule = $null

$FireWallRule = netsh advfirewall firewall show rule $RuleName
if ($FireWallRule -match "Rule Name") {
    $FireWallRuleIP = ($FireWallRule | Select-String -Pattern "^RemoteIP:.*?([0-9a-z].+$)").Matches |
        Foreach-Object { $_.groups[1].Value }
    $FirewallRulePort = ($FireWallRule | Select-String -Pattern "^LocalPort:.*?([0-9a-z].+$)").Matches |
        Foreach-Object { $_.groups[1].Value }
    $FireWallRuleDirection = ($FireWallRule | Select-String -Pattern "^Direction:.*?([0-9a-z].+$)").Matches |
        Foreach-Object { $_.groups[1].Value }
    $FireWallRuleAction = ($FireWallRule | Select-String -Pattern "^Action:.*?([0-9a-z].+$)").Matches |
        Foreach-Object { $_.groups[1].Value }
    $FirewallRuleEnabled = ($FireWallRule | Select-String -Pattern "^Enabled:.*?([0-9a-z].+$)").Matches |
        Foreach-Object { $_.groups[1].Value }

    if ($FirewallRuleDirection -eq "In" -and $FirewallRuleAction -eq "Allow" -and $FirewallRuleEnabled -eq "Yes" -and $FirewallRuleIP -like "$IPAddress*" -and $FirewallRulePort -eq $Port) {
            $Message = "Firewall rule $RuleName already exists and is configured correctly with:  Direction:Inbound, Action:Allow, Protocol:TCP, RemoteAddress:$IPAddress, LocalPort:$Port"
    }
        else {
            $null = netsh advfirewall firewall delete rule $RuleName
            $null = netsh advfirewall firewall add rule name=$RuleName dir=in protocol=TCP localport=$Port RemoteIP=$IPAddress action=allow
            $Message = "Firewall rule $RuleName was misconfigured.  It was deleted and recreated with:  Direction:Inbound, Action:Allow, Protocol:TCP, RemoteAddress:$IPAddress, LocalPort:$Port"
    }
}

答案 1 :(得分:0)

我发现的命令可以解决我的问题:

  1. 要获取防火墙规则:NetSH AdvFirewall Firewall show rule name=$RuleName
  2. 要添加防火墙规则:NetSH AdvFirewall Firewall Add Rule Name=$RuleName Dir=in Action=Allow Program=Any Enable=Yes RemoteIP=$IPAddress Protocol=TCP LocalPort=$Port
  3. 要删除防火墙规则,请执行以下操作:NetSH AdvFirewall Firewall Delete Rule Name=$RuleName

解决方案由@JeffZeitlin在评论中给出的链接确定