2017-02-15 65 views
1

當前要使用PowerShell腳本替換XML文件的from,to和subject屬性。使用PowerShell替換XML屬性

<configuration> 
<configSections> 
</configSections> 
<elmah> 
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="SqlServer" /> 
    <errorMail from="[email protected]" to="[email protected]" cc="" subject="Error email title"/> 
    <errorFilter> 
    <test> 
    </test> 
</errorFilter> 
</elmah> 

我曾嘗試以下foreach循環,但這似乎沒有更新的屬性:

$xmlDoc = [XML](Get-Content "$path\Web.config") 
foreach ($item in $xmlDoc.configuration.elmah.errorMail){ 
{$item.from = '[email protected]'} 
{$item.to = '[email protected]'} 
{$item.subject = 'test3'} 
} 
$xmlDoc.Save("$path\Web.config") 

任何人使用此腳本幫助嗎?

回答

1

你是將任務包裝在一個腳本塊中,這可能會引入新變量並阻止xml中的屬性更新。所以只需要刪除大括號

$xmlDoc = [XML](Get-Content "$path\Web.config") 
foreach ($item in $xmlDoc.configuration.elmah.errorMail) 
{ 
    $item.from = '[email protected]' 
    $item.to = '[email protected]' 
    $item.subject = 'test3' 
} 
$xmlDoc.Save("$path\Web.config")