使用PowerShell 2操作Windows防火墙规则
只要安装的PowerShell版本为4+,我就有PowerShell代码来操作Windows防火墙规则。但我需要在装有PowerShell 2的windows服务器上运行这些命令。我读过的所有内容都指向我使用使用PowerShell 2操作Windows防火墙规则,powershell,firewall,Powershell,Firewall,只要安装的PowerShell版本为4+,我就有PowerShell代码来操作Windows防火墙规则。但我需要在装有PowerShell 2的windows服务器上运行这些命令。我读过的所有内容都指向我使用Netshadvfirewall,但我没有找到正确的方法来满足我的需要 以下是我需要转换为PowerShell 2命令的5个PowerShell命令: 获取NetFirewallRule 获取NetFirewallAddressFilter 获取NetFirewallPortFilter
Netshadvfirewall
,但我没有找到正确的方法来满足我的需要
以下是我需要转换为PowerShell 2命令的5个PowerShell命令:
获取NetFirewallRule
获取NetFirewallAddressFilter
获取NetFirewallPortFilter
删除NetFirewallRule
新的NetFirewallRule
$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"
}
}
我认为这将有助于翻译:
$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"
}
}
我找到的解决我的问题的命令:
NetSH AdvFirewall firewall show rule name=$RuleName
NetSH AdvFirewall firewall add rule Name=$RuleName Dir=in Action=Allow Program=Any Enable=Yes RemoteIP=$IPAddress Protocol=TCP LocalPort=$Port
NetSH AdvFirewall firewall delete rule Name=$RuleName
解决方案是由@JeffZeitlin在评论中给出的链接决定的是否有用?@JeffZeitlin您的链接为我提供了解决问题所需的准确信息。命令如何工作的实际示例。谢谢