首页 > 解决方案 > 使用 new-netfirewallrule 添加防火墙规则时如何从文本文件中读取

问题描述

我正在尝试创建一个脚本以从文本文件中读取,我想将防火墙规则逐行添加到每个服务器。

它似乎不是逐行读取它,它看起来只取我输入的第一行。

$ComputersPath = "C:\temp\kofaxcomputers.txt"

Get-Content $ComputersPath | ForEach {

if ($ComputersPath -ne $null) {

        New-NetFirewallRule -DisplayName "Kofax - KTM" -Direction Inbound -RemoteAddress Any -Action Allow -Protocol TCP -LocalPort 2424
        Write-Host "$_ installed" -ForegroundColor Green;
        } else {
        Write-Host "$_ failed" -ForegroundColor Red;
        }}

标签: powershell-4.0powershell-remotingwindows-firewall

解决方案


如果您尝试在一堆计算机中创建防火墙规则,

  • 在你的文本文件中有一个计算机列表(确保没有尾随空格)
  • 从文本文件中读取所有计算机并将其保存在变量中。
  • 用于在所有这些计算机上Invoke-command执行。New-NetFireWallRule

     $Computers = get-Content -Path "C:\temp\kofaxcomputers.txt"
    
     Invoke-Command -ComputerName $Computers -ScriptBlock {
         New-NetFirewallRule -DisplayName "Kofax - KTM" -Direction Inbound -RemoteAddress Any -Action Allow -Protocol TCP -LocalPort 2424
     }
    

推荐阅读