2015-05-20 40 views
0

我在尝试使用数组的值更新XML文件中的URL。数组的输出值到文件中的字符串

我将计算机名输出到文本文件。我需要读取该文本文件并将内容输入到XML文件中的字符串中。

代码:

$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" 

我的问题是它的输入字$name到字符串,而不是价值。

我已经试过:

$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" 

这个错误有 “意外的标记”。

我也试过:

$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" 

这种错误与 “后失踪表达 ''”。

我很确定我没有正确地转义数组,所以它知道它的数组而不仅仅是一个值。

回答

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

PSH变量没有在单引号面条膨胀。

尝试使用双引号字符串,而不是:

$_ -replace '//twscheduler/',"//$name/" 
+0

PERFECT!非常感谢你。工作完美无瑕! –

+0

对不起,现在就完成了。再次感谢理查德 –