我发现Get-NetFirewallRule
cmdlet显示参数的方式有所不同。为了演示,我在防火墙中添加了“123.exe”(阻止操作)。 cmdlet给出了参数值的这种映射(为了空间的紧凑性,我减少了一些参数):
$Rules = [HashTable]::Synchronized(@{})
$Rules.Get = (Get-NetFirewallRule).Where({ $_.Action -in 'Block', 4 })
$Rules.Get
------------
DisplayName : 123
DisplayGroup :
Group :
Enabled : True
Profile : Domain, Private, Public
Platform : {}
Direction : Outbound
Action : Block
EdgeTraversalPolicy : Block
LooseSourceMapping : False
LocalOnlyMapping : False
然后,为了进行实验,我将cmdlet放置在不同的空间中,并获得了完全不同的参数显示,该参数现在没有Profile(我看到的是Profiles,而数字为7):
$Rules = [HashTable]::Synchronized(@{})
$RS = [Runspacefactory]::CreateRunspace(); $RS.Open()
$RS.SessionStateProxy.SetVariable('Rules', $Rules)
$PS = [PowerShell]::Create().AddScript({
$Rules.Get = (Get-NetFirewallRule).Where({ $_.Action -in 'Block', 4 })
})
$PS.Runspace = $RS
$Null = $PS.Invoke()
$Rules.Get
------------
Action : 4
Direction : 2
DisplayGroup :
DisplayName : 123
EdgeTraversalPolicy : 0
EnforcementStatus : {0}
LocalOnlyMapping : False
LooseSourceMapping : False
Platforms : {}
PolicyStoreSource : PersistentStore
PolicyStoreSourceType : 1
PrimaryStatus : 1
Profiles : 7
好吧,我添加了
-All
。我再次获得了方便的信息显示:$Rules = [HashTable]::Synchronized(@{})
$RS = [Runspacefactory]::CreateRunspace(); $RS.Open()
$RS.SessionStateProxy.SetVariable('Rules', $Rules)
$PS = [PowerShell]::Create().AddScript({
$Rules.Get = (Get-NetFirewallRule -All).Where({ $_.Action -in 'Block', 4 })
})
$PS.Runspace = $RS
$Null = $PS.Invoke()
$Rules.Get
------------
DisplayName : 123
DisplayGroup :
Group :
Enabled : True
Profile : Domain, Private, Public
Platform : {}
Direction : Outbound
Action : Block
EdgeTraversalPolicy : Block
LooseSourceMapping : False
LocalOnlyMapping : False
我关闭了PowerShell ISE,然后重新打开。我输入了相同的脚本并得到了预期的结果。我再次得到数字作为值:
$Rules = [HashTable]::Synchronized(@{})
$RS = [Runspacefactory]::CreateRunspace(); $RS.Open()
$RS.SessionStateProxy.SetVariable('Rules', $Rules)
$PS = [PowerShell]::Create().AddScript({
$Rules.Get = (Get-NetFirewallRule -All).Where({ $_.Action -in 'Block', 4 })
})
$PS.Runspace = $RS
$Null = $PS.Invoke()
$Rules.Get
------------
Action : 4
Direction : 2
DisplayGroup :
DisplayName : 123
EdgeTraversalPolicy : 0
EnforcementStatus : {0}
LocalOnlyMapping : False
LooseSourceMapping : False
Platforms : {}
PolicyStoreSource : PersistentStore
PolicyStoreSourceType : 1
PrimaryStatus : 1
Profiles : 7
问题:如何始终获得这样的参数显示?
------------
DisplayName : 123
DisplayGroup :
Group :
Enabled : True
Profile : Domain, Private, Public
Platform : {}
Direction : Outbound
Action : Block
EdgeTraversalPolicy : Block
LooseSourceMapping : False
LocalOnlyMapping : False
谢谢
最佳答案
如果您修改对Get-NetFirewallRule的调用以包括选择要查找的属性,它将为您获取所需的数据。
Get-NetFirewallRule -All | select DisplayName, DisplayGroup, Group, Enabled, Profile, platform, direction, action, edgetraversalpolicy, looseSourceMapping, localonlymapping
如果您要生成表(例如 View )或
format-table
(用于显示形式),则可以将其输送到Out-GridView
。$Rules = [HashTable]::Synchronized(@{})
$RS = [Runspacefactory]::CreateRunspace(); $RS.Open()
$RS.SessionStateProxy.SetVariable('Rules', $Rules)
$PS = [PowerShell]::Create().AddScript({
$Rules.Get = (Get-NetFirewallRule -All | select DisplayName, DisplayGroup, Group, Enabled, Profile, platform, direction, action, edgetraversalpolicy, looseSourceMapping, localonlymapping) | ? { $_.Action -in 'Block', 4 }
})
$PS.Runspace = $RS
$Null = $PS.Invoke()
$Rules.Get
关于powershell - Get-NetFirewallRule的奇怪行为,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60249747/