【问题标题】:Powershell foreach loop reading csv, manipulating and writing out to txtPowershell foreach循环读取csv,操作并写入txt
【发布时间】:2021-02-18 07:05:22
【问题描述】:

编辑:@Theo 回答

感谢 Theo,对 1000+ 行输入文件非常有效。我会用谷歌检查所有这些命令,这样我就可以开始了解你做了什么/你是怎么做的,但这一切都很好,再次感谢!

:编辑

抱歉有任何格式错误,这是我的第一个问题。

我正在尝试通过使用 Powershell 从要阻止的 URL 的 csv(或 txt)文件中读取来为 Fortigate Firewall 编写脚本。

在 Stack Overflow 上的各种帖子、示例输入、预期输出和实际输出的帮助下,我自己编写了当前代码。

在下面标记的部分中: 这就是我想要的输出

这是我在txt文件中得到的输出

有一些不应该存在的行空间,但我无法正确格式化,如果我删除要发布的行,它会给我一个单行,所有内容都混乱了。

谁能告诉我代码哪里出了问题?

提前非常感谢, 铝

到目前为止,我有以下代码:

$fileout = "C:\ScriptTests\testcli.txt"

if (Test-Path $fileout) {
  Remove-Item $fileout
}

Add-Content -Path $fileout -Value "config webfilter urlfilter"
Add-Content -Path $fileout -Value "edit 1"
$nameset = "set name " + '"Test URL-Block"'
Add-Content -Path $fileout -Value $nameset
Add-Content -Path $fileout -Value "config entries"

$filein = "C:\ScriptTests\testblock.csv"
$counter1 = 1

Get-Content $filein

$contentout = 
    ForEach($fline in $filein)
    {
    $editnum = 'edit' + $counter1
    $urlblock = 'set url ' + '"*.' + $fline + '"'     #set url Quote Asterisk dot object-from-csv endquote eg: "*.zzz.com" or "*.1.2.3.4"
    $wild = 'set type wildcard'  #output set type wildcard
    $act = 'set action block'   #output set action block
    $nxt = 'next' #output next
    
    $editnum
    $urlblock
    $wild
    $act
    $nxt
    $counter1 = $counter1 + 1
}

$contentout | Out-File $fileout -Append
Add-Content -Path $fileout -Value "end"   #output end
Add-Content -Path $fileout -Value "end"   #output end

输入文件包含(当前)4 行,但可能超过 1000 行。

  1. 111.222.223.224
  2. 8.8.8.8
  3. zzz.com
  4. defabc.com.au

这就是我想要的输出:

配置 webfilter urlfilter

编辑 1

设置名称“测试 URL 块”

配置条目

编辑 1

设置网址“*.111.222.223.224”

设置类型通配符

设置动作块

下一个

编辑 2

设置网址“*.8.8.8.8”

设置类型通配符

设置动作块

下一个

编辑 3

设置网址“*.zzz.com”

设置类型通配符

设置动作块

下一个

编辑 4

设置网址“*.defabc.com.au”

设置类型通配符

设置动作块

下一个

结束

结束

这是我在 txt 文件中得到的输出:

配置 webfilter urlfilter

编辑 1

设置名称“测试 URL 块”

配置条目

编辑 1

s e t u r l " * . C : \ S c r i p t T e s t s \ t e s t b l o c k . c s v "

s e t t y p e w i l d c a r d

s e t a c t i o n b l o c k

n e x t

结束

结束

【问题讨论】:

  • 为什么不使用Import-Csv cmdlet 来加载csv 数据?这比使用Get-Content 加载行要好得多... [grin]
  • 你有奇怪的间距,因为你正在混合编码。 Get-Content 将处理编码,但 Out-File 在 Windows PowerShell 中默认为 UTF-16le。最好在任何命令上使用-Encoding 开关。
  • @Lee_Dailey - Import-Csv - 给出与Get-Content相同的结果
  • @AdminOfThings - 感谢您的解释。尝试使用-Encoding 输出中仍然有文件名。
  • @AlJames - 啊!我误解了您输入文件的布局。我看到你有一个修复,虽然...太棒了! [咧嘴]

标签: powershell


【解决方案1】:

我会为此使用 Here-Strings:

$filein  = "C:\ScriptTests\testblock.csv"
$fileout = "C:\ScriptTests\testcli.txt"

# create a Here-String for the edits
$edit = @"
edit {0}
set url "{1}"
set type wildcard
set action block
next
"@

# create another Here-String for the complete file
$content = @"
config webfilter urlfilter
edit 1
set name "Test URL-Block"
config entries
{0}
end
"@

$counter = 1
$editBlocks = Get-Content $filein | ForEach-Object {
    # output the textblock with placeholders {0} and {1} filled in
    $edit -f $counter++, "*.$_"
}

# output the completed file with placeholder {0} replaced by the blocks, which are joined together with a Newline
# -PassThru will also display the completed text on screen. If you don't want that, remove that switch.
$content -f ($editBlocks -join [Environment]::NewLine) | Set-Content -Path $fileout -Force -PassThru

输出:

配置 webfilter urlfilter 编辑 1 设置名称“测试 URL 块” 配置条目 编辑 1 设置网址“*.111.222.223.224” 设置类型通配符 设置动作块 下一个 编辑 2 设置网址“*.8.8.8.8” 设置类型通配符 设置动作块 下一个 编辑 3 设置网址“*.zzz.com” 设置类型通配符 设置动作块 下一个 编辑 4 设置网址“*.defabc.com.au” 设置类型通配符 设置动作块 下一个 结尾

我不知道为什么你想要的输出中有一个额外的end。如果需要,只需将它添加到$content Here-String

【讨论】:

  • 谢谢@Theo,可以完美地处理超过 1000 行的输入文件。我会用谷歌检查所有这些命令,这样我就可以开始了解你做了什么/你是怎么做的,但这一切都很好,再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 2012-12-01
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
  • 2013-08-28
  • 1970-01-01
相关资源
最近更新 更多