Daniel4711
Daniel4711

Reputation: 422

get-content -multiple Replacements

At First: Sorry for my Bad Englisch.

The Question:

  1. I have an .csv File with Name, Email-Adresse etc. of 30 Users
  2. I have an hmtl (or rtf or txt) with a Placeholder for Name (NameOfUser) and the Email Adress (EmailUser) (somewhere and sometimes multiple in the document).
  3. I want to read the csv File an for Each Username/Email i want to: a) Replace the Word NameOfUser with the $User.Username entry of the csv File b) Replace the Word EmailUser with the $User.Email entry of the csv File c) write a new file with the new Content to \UNC\Username\New-html-file.htm

My Problem: If i only Change one thing, Name or Email Adresse, the Output Works. If i put a second -replace into the foreach-object part, the output results in an error. I have to change 5 different Entrys and hope that i can fix it in one get-content | ..... step.

A Part of the Script:

$userfile = import-csv \\the-fileserver\User.csv
foreach ($User in $Userfile) {

   Get-Content \\the-fileserver\File.htm | foreach-object { 
            $_ -replace 'Username', $User.Name 
            $_ -replace 'email-adresse', $User.Email

             } | set-content -path $NewHTMFile

Problem: Each line appears 2 (or more equal to the -replace entries) Times in the $NewHTMFile :(

Thanks in Advance Daniel

Upvotes: 1

Views: 284

Answers (2)

mjolinor
mjolinor

Reputation: 68293

Not tested, but I think this shouild work, and avoid a some chewing in the disk drive:

$userfile = import-csv \\the-fileserver\User.csv
$HTML = Get-Content '\\the-fileserver\File.htm'

foreach ($User in $Userfile)
  {
    $HTML -replace 'Username', $User.Name -replace 'email-adresse', $User.Email |
     set-content -path $NewHTMFile
  }

Upvotes: 1

CB.
CB.

Reputation: 60928

try

...
Get-Content \\the-fileserver\File.htm | foreach-object { 
            $_ -replace 'Username', $User.Name -replace 'email-adresse', $User.Email }
...

Upvotes: 1

Related Questions