首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Powershell Get-NetFirewallRule规则确定Windows防火墙规则的程序路径

如何使用Powershell Get-NetFirewallRule规则确定Windows防火墙规则的程序路径
EN

Stack Overflow用户
提问于 2016-02-13 21:30:27
回答 1查看 4.7K关注 0票数 7

我们为某些程序定义了一个新的Windows防火墙规则,用于在某个端口上接受入站TCP连接。这可以使用netsh.exe实用程序或Powershell NetFirewallRule来完成。例如,下面的示例命令允许notepad.exe接受端口5001上的TCP连接(我知道记事本不能这样做):

代码语言:javascript
运行
复制
New-NetFirewallRule  -program "C:\windows\System32\notepad.exe" -direction Inbound -Action Allow -Protocol tcp -LocalPort 5001 -Name "Testing Notepad on port 5001" -DisplayName "Testing Notepad on port 5001"

要检索/查看此规则,可以再次使用netsh.exe或Get-NetFirewallRule。

理想情况下,我们希望使用Powershell Get-NetFirewallRule,但我们无法查看创建规则时使用的实际程序路径。

下面是netsh.exe的输出:

代码语言:javascript
运行
复制
netsh advfirewall firewall show rule name="Testing Notepad on port 5001" verbose

Rule Name:                            Testing Notepad on port 5001
----------------------------------------------------------------------
Enabled:                              Yes
Direction:                            In
Profiles:                             Domain,Private,Public
Grouping:
LocalIP:                              Any
RemoteIP:                             Any
Protocol:                             TCP
LocalPort:                            5001
RemotePort:                           Any
Edge traversal:                       No
Program:                              C:\windows\System32\notepad.exe
InterfaceTypes:                       Any
Security:                             NotRequired
Rule source:                          Local Setting
Action:                               Allow
Ok.

以下是Get-NetFirewallRule的输出:

代码语言:javascript
运行
复制
Get-NetFirewallRule -Name "Testing Notepad on port 5001" | Format-list *

Name                    : Testing Notepad on port 5001
ID                      : Testing Notepad on port 5001
Group                   :
Platform                : {}
LSM                     : False
DisplayName             : Testing Notepad on port 5001
Enabled                 : True
Profile                 : Any
Direction               : Inbound
Action                  : Allow
EdgeTraversalPolicy     : Block
PrimaryStatus           : OK
Status                  : The rule was parsed successfully from the store.
                          (65536)
EnforcementStatus       : NotApplicable
PolicyStoreSourceType   : Local
Caption                 :
Description             :
ElementName             : Testing Notepad on port 5001
InstanceID              : Testing Notepad on port 5001
CommonName              :
PolicyKeywords          :
PolicyDecisionStrategy  : 2
PolicyRoles             :
ConditionListType       : 3
CreationClassName       : MSFT|FW|FirewallRule|Testing Notepad on port 5001
ExecutionStrategy       : 2
Mandatory               :
PolicyRuleName          :
Priority                :
RuleUsage               :
SequencedActions        : 3
SystemCreationClassName :
SystemName              :
DisplayGroup            :
LocalOnlyMapping        : False
LooseSourceMapping      : False
Owner                   :
Platforms               : {}
PolicyStoreSource       : PersistentStore
Profiles                : 0
RuleGroup               :
StatusCode              : 65536
PSComputerName          :
CimClass                : root/standardcimv2:MSFT_NetFirewallRule
CimInstanceProperties   : {Caption, Description, ElementName, InstanceID...}
CimSystemProperties     : Microsoft.Management.Infrastructure.CimSystemPropertieses

对于使用Powershell cmdlet检索程序路径、端口、协议等,有什么建议或想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-13 22:37:59

为此,您应该使用Get-NetFirewall*Filter cmdlet。

代码语言:javascript
运行
复制
PS> Get-Command Get-NetFirewall*Filter

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Get-NetFirewallAddressFilter                       2.0.0.0    NetSecurity
Function        Get-NetFirewallApplicationFilter                   2.0.0.0    NetSecurity
Function        Get-NetFirewallInterfaceFilter                     2.0.0.0    NetSecurity
Function        Get-NetFirewallInterfaceTypeFilter                 2.0.0.0    NetSecurity
Function        Get-NetFirewallPortFilter                          2.0.0.0    NetSecurity
Function        Get-NetFirewallSecurityFilter                      2.0.0.0    NetSecurity
Function        Get-NetFirewallServiceFilter                       2.0.0.0    NetSecurity

所有这些cmdlet都有-AssociatedNetFirewallRule参数,它接受管道输入。

在这种情况下,可以使用以下命令:

代码语言:javascript
运行
复制
Get-NetFirewallRule -Name "Testing Notepad on port 5001" | Get-NetFirewallApplicationFilter
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35385551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档