我们为某些程序定义了新的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?
谢谢 - 根据需要完美的作品! – alexg