Powershell 开放端口配置文件,全部(私有(private)公共(public)域)

标签 powershell

我有一个打开端口 5555 的 powershell 脚本,但是当我希望它全部(私有(private)、公共(public)、域)时,它默认为 profile = private。如何修改脚本以实现此目的?

$port = New-Object -ComObject HNetCfg.FWOpenPort
$port.Port = 5555
$port.Name = 'MyPort'
$port.Enabled = $true

$fwMgr = New-Object -ComObject HNetCfg.FwMgr
$profile = $fwMgr.LocalPolicy.CurrentProfile
$profile.GloballyOpenPorts.Add($port)

$port = New-Object -ComObject HNetCfg.FWOpenPort
$port.Port = 6521
$port.Name = 'ArkleSQL'
$port.Enabled = $true

$fwMgr = New-Object -ComObject HNetCfg.FwMgr
$profile = $fwMgr.LocalPolicy.CurrentProfile
$profile.GloballyOpenPorts.Add($port)

最佳答案

您可以使用 FwPolicy2FWRule为所有配置文件创建规则:

$fwPolicy = New-Object -ComObject HNetCfg.FwPolicy2

$rule = New-Object -ComObject HNetCfg.FWRule
$rule.Name = 'MyPort'
$rule.Profiles = $NET_FW_PROFILE2_ALL
$rule.Enabled = $true
$rule.Action = $NET_FW_ACTION_ALLOW
$rule.Direction = $NET_FW_RULE_DIR_IN
$rule.Protocol = $NET_FW_IP_PROTOCOL_TCP
$rule.LocalPorts = 5555

$fwPolicy.Rules.Add($rule)

以下是使用的常量:
$NET_FW_PROFILE2_DOMAIN = 1
$NET_FW_PROFILE2_PRIVATE = 2
$NET_FW_PROFILE2_PUBLIC = 4
$NET_FW_PROFILE2_ALL = 2147483647

$NET_FW_IP_PROTOCOL_TCP = 6
$NET_FW_IP_PROTOCOL_UDP = 17
$NET_FW_IP_PROTOCOL_ICMPv4 = 1
$NET_FW_IP_PROTOCOL_ICMPv6 = 58

$NET_FW_RULE_DIR_IN = 1
$NET_FW_RULE_DIR_OUT = 2

$NET_FW_ACTION_BLOCK = 0
$NET_FW_ACTION_ALLOW = 1

(来源:http://www.ohmancorp.com/files/RefWin-AdvFirewall-JCopyFWRules.txt)

关于Powershell 开放端口配置文件,全部(私有(private)公共(public)域),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12673368/

相关文章:

regex - Powershell正则表达式删除逗号但不删除分隔符

powershell - Powershell move 项不会从源中删除某些3D png文件

powershell - 忽略输出-它不应在屏幕上打印

c# - 带有StreamWriter的StreamReader显示性能较低的PowerShell,但File.ReadLines

Powershell:如何将字节字符串与二进制注册表值进行比较

regex - 选择字符串以仅显示返回行的特定部分

Powershell if 后缺少语句 block

powershell - PowerShell 是否支持 PS 模块中的私有(private)静态变量

powershell - 从非 powershell 终端传递 bool powershell 参数

Powershell - 将文件夹的结构导出到 txt 或 xls,但只能导出 3 个文件夹的深度且没有文件