powershell-4.0 - 使用 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;
}}
解决方案
如果您尝试在一堆计算机中创建防火墙规则,
- 在你的文本文件中有一个计算机列表(确保没有尾随空格)
- 从文本文件中读取所有计算机并将其保存在变量中。
用于在所有这些计算机上
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 }
推荐阅读
- javascript - Javascript 获取变量问题
- python - 如何在给定点数的情况下绘制最佳椭圆?
- postgresql - 如何在 postgreapp 中为 PostgreSQL 设置密码?
- javascript - 如何在 Javascript 中将对象的所有键设置为 newValue 而不会发生变异(Redux)?
- javascript - 将数据从 React 组件传递到 MVC 控制器
- macos - 应用因使用全局热键而被拒绝
- aframe - 如何在框架中使用 CSS3DRenderer?
- javascript - 使用动态名称访问对象中的元素
- python-3.x - 文件上传“请求的资源不存在。” [欧特克现实捕捉]
- docker - 如何确保 remote-exec provisioner 环境与直接运行 ssh 相同?