I've got the following PowerShell script (I'm using Powershell v5.1), which I took and adapted mostly from this previous post: Replace multiline text in a file using Powershell without using Regex:
$oldCode = @"
<httpProtocol>
<customHeaders>
<clear />
</customHeaders>
<redirectHeaders>
<clear />
</redirectHeaders>
</httpProtocol>
"@
$newCode = @"
<httpProtocol>
<customHeaders>
<add name="X-Frame-Options" VALUE="SAMEORIGIN"></add>
</customHeaders>
<redirectHeaders>
<clear />
</redirectHeaders>
</httpProtocol>
"@
$Path = "c:\Windows\System32\inetsrv\config\applicationHost.config"
$Content = (Get-Content $Path -Raw).replace($oldCode,$newCode)
Set-Content -Path $Path -Value $Content -Verbose
This doesn't replace the $oldCode, however. I've used Write-Output to check the $Content variable and it isn't replacing the string, so I'm assuming it's a problem matching the string or with the replace command itself, rather than a problem with the Set-Content command.
Any ideas on how to get this working?