1

Currently want to replace the from, to and subject attributes of an XML file using a PowerShell script.

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

I have tried the following foreach loop but this doesnt seem update the attributes:

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

Anyone help with this script?

1 Answer 1

1

You are wrapping the assignment in a scriptblock which will probably introduce new variables and prevent the properties in the xml from beeing updated. So just remove the curly brackets:

$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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.