首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在powershell中添加多个防火墙规则而不创建重复条目

在powershell中添加多个防火墙规则而不创建重复条目
EN

Stack Overflow用户
提问于 2021-06-18 07:50:33
回答 1查看 72关注 0票数 2

我正在尝试创建多个防火墙规则,但如果该规则已经存在,我不想创建重复的规则。

目前,我的脚本将创建第一个规则,但是当它遍历循环时,它总是会说已经有一个重复的规则,所以它将停止。有没有一种方法可以应用数组中的所有规则?

代码语言:javascript
复制
$c = "wsbxlcfe101"

$fwNames = @("ECMP - OutSystems LifeTime", "ECMP - OutSystems Deployment Controller")

foreach($name in $fwNames){

    $session = New-PSSession -ComputerName $c

    $ifFirewallExists = Invoke-Command -Session $session -ScriptBlock {              
        Get-NetFirewallRule -Direction Inbound -ErrorAction SilentlyContinue | where DisplayName -Match "ECMP" | select DisplayName, Enabled, Direction, Action
    }
    if ($ifFirewallExists.DisplayName){
        Write-Host "firewall rules already created." -ForegroundColor Red  
    }            
    else {
        New-NetFirewallRule -DisplayName $name -Direction Inbound -RemoteAddress Any -Action Allow -Protocol TCP -LocalPort "80","443"
        Write-Host "Rules created." -ForegroundColor Green
    }
    Remove-PSSession -Session $session         
}

编辑*

下面是更新后的代码,看起来可以正常工作

代码语言:javascript
复制
try {

    $c = "wsbxlcfe101"
  
    $session = New-PSSession -ComputerName $c

    Invoke-Command -ScriptBlock { 
    
        $fwNames = @("ECMP - OutSystems LifeTime", "ECMP - OutSystems Deployment Controller")

        foreach($name in $fwNames){       
    
                $FirewallRules = Get-NetFirewallRule -Direction Inbound -ErrorAction SilentlyContinue 

                if ($FirewallRules.DisplayName -eq $name){
                    Write-Host "firewall rules already created." -ForegroundColor Red  
                }            
                else {
                    New-NetFirewallRule -DisplayName $name -Direction Inbound -RemoteAddress Any -Action Allow -Protocol TCP -LocalPort "80","443"
                    Write-Host "$name rule created." -ForegroundColor Green
                }
        }
       
    } -Session $session
}
catch {
    Write-Host $_ -ForegroundColor Red
}
finally {
    Get-PSSession | Remove-PSSession 
}
EN

回答 1

Stack Overflow用户

发布于 2021-06-18 10:25:53

在不修改太多代码的情况下:

代码语言:javascript
复制
Try {

    #$c = "wsbxlcfe101"X
    $PSSession = New-PSSession -ComputerName "wsbxlcfe101" -ErrorAction Stop


    Invoke-Command -ScriptBlock {

        $FireWallRules = Get-NetFirewallRule -Direction Inbound

        $fwNames = @("ECMP - OutSystems LifeTime", "ECMP - OutSystems Deployment Controller")
            foreach ($Name in $fwNames) {

                foreach ($Rule in $FireWallRules) {

                    if ($Name -match $Rule.DisplayName) {

                        "Firewall Rule [$Name] already exists!"

                    }
                    else {
                        
                        New-NetFirewallRule -DisplayName $Name `
                                            -Direction Inbound `
                                            -RemoteAddress Any `
                                            -Action Allow `
                                            -Protocol TCP `
                                            -LocalPort "80","443"

                        "Firewall Rule [$Name] Created."

                    }

                }

            }

    } -Session $PSSession
} 
Catch [System.Management.Automation.Remoting.PSRemotingTransportException] {

    $Error[0].Exception.Message.Split('.')[1].Trim()

} 
Finally {

    Get-PSSession | Remove-PSSession

}

您要做的是使用-Match运算符检查"ECMP“,这将针对名称中包含"ECMP”的任何内容测试true。这就是为什么您会收到防火墙规则已经创建的消息。现在,让我们回到一些powershell的基础知识。

  1. 让我们将代码封装到TryCatch块中,以便在您无法建立到远程PC的连接时捕获抛出的异常。
  2. 您当前正在以您拥有的方式创建PSSession,这是我们认为的“计算成本很高”。这样想吧。对于数组中的每个名称,为同一台计算机创建一个新会话,然后重新运行相同的命令以检查数组中的每个值的数量。创建一个会话就足够了。
  3. 您可以将整个命令发送到远程计算机来完成这一端的工作,而不是必须将其序列化为对象,然后将其发送到您的计算机,对其进行测试,然后重复该过程;这又回到了“计算成本很高”的状态。

这允许您创建one PSSession,并发送命令just once,而不会占用您的计算机资源。请注意,这还没有经过测试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68027736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档