2012-10-01 75 views
4

我有一個PowerShell腳本,打開端口5555,但它默認爲配置文件=私人,當我希望它是所有(私人,公共,域)。我如何修改腳本來實現這一目標?Powershell打開端口配置文件,所有(私有公共域)

$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) 

回答

2

有兩個值用於防火牆管理的COM對象。 0代表域網絡,1代表標準網絡。 (在此API中)似乎沒有區分這種公共和私人配置文件。

您可以

$Profiles = @{ 
    NET_FW_PROFILE_DOMAIN = 0 
    NET_FW_PROFILE_STANDARD = 1 
} 

$fwMgr = New-Object -ComObject HNetCfg.FwMgr 

$profile.GloballyOpenPorts.Add($port) 
foreach ($ProfileKey in $Profiles.Keys) 
{ 
    $Profile = $fwMgr.LocalPolicy.GetProfileByType($profiles[$ProfileKey]) 
    $Profile.GloballyOpenPorts.Add($Port) 
} 
1

取代

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

最後一節可以使用FwPolicy2FWRule創建所有配置的規則:

這裏有常量:

$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