6

我知道我可以使用netsh advfirewall firewall add rulewf.msc创建新的防火墙规则;但是当我以这种方式创建规则时,它不会有groupName,因此我无法同时管理多个规则。

有没有办法为某些防火墙规则指定组名

4

4 回答 4

7

以下 Powershell 单行代码为 DisplayName='GTA V' 的规则添加/重命名组名称。适用于 Windows 8+

Get-NetFirewallRule -DisplayName 'GTA V' | ForEach { $_.Group = 'games'; Set-NetFirewallRule -InputObject $_ }

于 2017-07-16T13:21:18.570 回答
6

好的!我知道我现在应该怎么做 :)
使用PowerShellWMI COM-Objects我可以做我想做的事!
这是ps1我编写的用于添加防火墙规则的脚本,以便我可以SQL Server远程连接到我的。

function isFirewallPortOpen {
    param( [int] $port )
    $fw = New-Object -ComObject hnetcfg.fwpolicy2 
    if ($fw.Rules | Where {$_.LocalPorts -eq $port }) {
        return [bool]$true
    } else {
        return [bool]$false
    }
}

function existsFirewallRule {
    param( [string] $name )
    $fw = New-Object -ComObject hnetcfg.fwpolicy2 
    if ($fw.Rules | Where { $_.Name -eq $name }) {
        return [bool]$true
    } else {
        return [bool]$false
    }
}

function addFirewallRule {
    param(
        [string] $name,
        [int] $port,
        [int] $protocol
    )
    $fw = New-Object -ComObject hnetcfg.fwpolicy2 
    if (isFirewallPortOpen $port -or existsFirewallRule $name) {
        Write-Host -ForegroundColor:Red "**Rule Already Exists or Port Already Open."
    } else {
        $rule = New-Object -ComObject HNetCfg.FWRule

        $rule.Name = $name
        $rule.Protocol = $protocol # 6=NET_FW_IP_PROTOCOL_TCP and 17=NET_FW_IP_PROTOCOL_UDP
        $rule.LocalPorts = $port
        $rule.Enabled = $true
        $rule.Grouping = "SQL Server"
        $rule.Profiles = 7 # all
        $rule.Action = 1 # NET_FW_ACTION_ALLOW
        $rule.EdgeTraversal = $false

        $fw.Rules.Add($rule)
        Write-Host -ForegroundColor:Blue "A rule named '$name' has been added to Windows' Firewall."
    }
}

addFirewallRule -name:"Transact SQL Debugger" -port:135 -protocol:6
addFirewallRule -name:"SQL Traffic" -port:1433 -protocol:6
addFirewallRule -name:"SQL Browser Traffic" -port:1434 -protocol:17
addFirewallRule -name:"SQL Analytics Traffic" -port:2383 -protocol:6
addFirewallRule -name:"SQL Broker Traffic" -port:4022 -protocol:6
于 2012-08-14T16:11:39.350 回答
1
$fw= New-Object -ComObject hnetcfg.fwpolicy2
$fw.rules | ? {$_.name -like "*sql*"} | ft name,grouping
$fw.rules | ? {$_.name -like "*sql*"} | % {$gn="_mssqlservices";$_.grouping = $gn}
$fw.rules | ? {$_.name -like "*sql*"} | ft name,grouping
于 2013-03-09T02:50:35.470 回答
0
$rule = Get-NetFirewallRule -Name 'nameme'; $rule.Group = 'nameme'; $rule | Set-NetFirewallRule; 
netsh advfirewall firewall show rule name=nameme
netsh advfirewall set allprofiles state on
netsh advfirewall set allgroups on
netsh advfirewall firewall add rule name="nameme" dir=out action=Allow
netsh advfirewall export "C:\temp\WFconfiguration.wfw"
netsh advfirewall import "C:\temp\WFconfiguration.wfw"

注意:您必须先在 Windows 防火墙中创建规则名称,然后更改nameme为名称运行 PowerShell 管理员。

对于组名=任何

截屏

管理 Windows 防火墙现在比以往任何时候都更容易 去下载吧

于 2018-08-18T00:03:04.463 回答