小编tje*_*gan的帖子

如何使用 Powershell 检查现有的防火墙规则

所以,我有这个脚本:

function Add-FirewallRule {
   param( 
      $name,
      $tcpPorts,
      $appName = $null,
      $serviceName = $null
   )
    $fw = New-Object -ComObject hnetcfg.fwpolicy2 
    $rule = New-Object -ComObject HNetCfg.FWRule

    $rule.Name = $name
    if ($appName -ne $null) { $rule.ApplicationName = $appName }
    if ($serviceName -ne $null) { $rule.serviceName = $serviceName }
    $rule.Protocol = 6 #NET_FW_IP_PROTOCOL_TCP
    $rule.LocalPorts = $tcpPorts
    $rule.Enabled = $true
    $rule.Grouping = "@firewallapi.dll,-23255"
    $rule.Profiles = 7 # all
    $rule.Action = 1 # NET_FW_ACTION_ALLOW
    $rule.EdgeTraversal = $false
    if(*here*)
    {
    $fw.Rules.Add($rule)
    }

}
Run Code Online (Sandbox Code Playgroud)

并且我希望能够在 if() 中放入一些内容,以便在添加规则之前检查该规则是否已经存在。我对powershell不是很熟悉,所以对我放轻松:P

powershell firewall

5
推荐指数
3
解决办法
2万
查看次数

试图在C#中运行powershell脚本

我有一个添加防火墙规则的PS脚本.

我正在使用Process.Start("powershell.exe"," - File"+"\"C:\ Program Files(x86)\ blah\blah\add-FirewallRule.ps1 \"");

此命令在我的本地计算机上运行正常,但在测试中我一直在Windows Server 2008 R2上运行该程序,并且PS脚本将无法运行.如果我从命令行手动运行它,它运行并正常工作,但我的代码将无法正常工作.所有文件路径都是正确的(我已经检查过并且有一个同事检查它).当我运行代码时,PS控制台甚至没有弹出.

有什么建议?

.net c# powershell

3
推荐指数
1
解决办法
3063
查看次数

标签 统计

powershell ×2

.net ×1

c# ×1

firewall ×1