0

I'm trying up update a URL in an XML file with the value of an array.

I'm outputting the computer name to a text file. I need to read that text file and input the contents into a string in an XML file.

Code:

$name = Get-Content 'C:\temp\hostname.txt'
(Get-Content "E:\Source\Igloo Upgrade to v5.0\Igloo Enterprise Distribution 5.0\Setup\Igloo Enterprise Administrator.SetParameters.xml") |
  ForEach-Object {$_ -replace '//twscheduler/','//$name/'} | 
  Set-Content "E:\Source\Igloo Upgrade to v5.0\Igloo Enterprise Distribution 5.0\Setup\Igloo Enterprise Administrator.SetParameters.xml"

The issue I have is it's inputting the word $name into the string instead of the value.

I've tried:

$name = Get-Content 'C:\temp\hostname.txt'
(Get-Content "E:\Source\Igloo Upgrade to v5.0\Igloo Enterprise Distribution 5.0\Setup\Igloo Enterprise Administrator.SetParameters.xml") |
  ForEach-Object {$_ -replace '//twscheduler/','//'$name'/'} | 
  Set-Content "E:\Source\Igloo Upgrade to v5.0\Igloo Enterprise Distribution 5.0\Setup\Igloo Enterprise Administrator.SetParameters.xml"

This errors with "unexpected token".

I've also tried:

$name = Get-Content 'C:\temp\hostname.txt'
(Get-Content "E:\Source\Igloo Upgrade to v5.0\Igloo Enterprise Distribution 5.0\Setup\Igloo Enterprise Administrator.SetParameters.xml") |
  ForEach-Object {$_ -replace '//twscheduler/',//$name/} | 
  Set-Content "E:\Source\Igloo Upgrade to v5.0\Igloo Enterprise Distribution 5.0\Setup\Igloo Enterprise Administrator.SetParameters.xml"

This errors with "missing expression after ','".

I'm pretty sure that I'm not escaping the array correctly so that it knows its an array and not just a value.

1 Answer 1

2
   $_ -replace '//twscheduler/','//$name/'

PSH variables are not expanded in single quoted strings.

Try using a double quoted string instead:

$_ -replace '//twscheduler/',"//$name/"
1
  • PERFECT!! Thank you very much. Worked flawlessly! Commented May 20, 2015 at 12:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.