对于 CSV 中的每个名称,写入每个输出文件,将单词 00000 替换为名称,例如 1 输出 Henry 等等

for each Name from CSV write to each output file replacing the word 00000 to name for example 1 output Henry and so on

我正在尝试使用每个(名称)在 CSV 中使用来替换写在我正在导出的每个文件上的 (00000)。
该文件已经包含单词 00000

csv 文件包含:

FullName             Name    LastWriteTime
\remotecomputer\    Henry   4/30/2020  3:44:57 PM
\remotecompter\     Magy    12/7/2020  9:04:28 PM

第一个 txt 应该是这样的 @回声关闭 if /i "%UserName%" neq "Henry" exit

第二个 txt 应该是这样的 @回声关闭 if /i "%UserName%" neq "Magy" exit

原来Config.txt是这样的 @回声关闭 如果 /i "%UserName%" neq "00000" 退出

代码:

$source = Read-Host -Prompt 'Insert source path'
Import-Csv C:$source-PREP.csv | ForEach-Object {$_.Name} |ForEach-Object {Get-Content c:\Config.txt | ForEach-Object {$_ -replace '00000',"$_.Name"} |Out-File c:$_.txt  -Force}
 

认为你想做的:

  • 读取包含 "00000" 作为占位符的模板文件 c:\Config.txt
  • 读取一个 csv 文件 C:\Something-PREP.csv,其中包含一个名为 Name
  • 的列
  • 使用这些名称替换模板中的 "00000" 占位符
  • 输出新文件C:\Henry.txtC:\Magy.txt

如果这个假设是正确的,试试

$template = Get-Content -Path 'C:\Config.txt'
(Import-Csv -Path 'C:\Whatever-PREP.csv').Name | ForEach-Object {
    # replace "00000" with the name (represented by `$_`) and use that name as filename for the output
    $template -replace '"00000"', ('"{0}"' -f $_) | Set-Content -Path ('C:\{0}.txt' -f $_) -Force
}

P.S。确保您导入的 CSV 使用 逗号 作为分隔符。如果这是其他字符,请将 -Delimiter '<yourCharacter>' 添加到 cmdlet。