2016-02-13 338 views
2

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

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 cmdlet。

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

这里的netsh.exe中的输出:

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. 

这里获取-NetFirewallRule cmdlet的输出:

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?

回答

4

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

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参数,它的管道输入。

在你的情况,你可以使用下面的命令:

Get-NetFirewallRule -Name "Testing Notepad on port 5001" | Get-NetFirewallApplicationFilter 
+0

谢谢 - 根据需要完美的作品! – alexg

相关问题