0

I am doing find and replace a string in attribute of xml file using powershell. The strings are properly replaced [I verified it by displaying

"After replace $($Item.Node.$attribute)"

] but in the config file it is not reflected. Seems like it is not saving with latest values properly. I tried all the possibilities but still mysteriously it is not saving. I am using powershell version 2.

function Set-XMLAttribute
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # webconfig file full path
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [string]$Path,

        # Param2 help description
        [string] $xPath,

        [string] $attribute,

        [string] $valueToFind,

        [string] $ValueToReplace
    )

    Begin { }
    Process
    {
        try 
        {

            If ( Test-path -Path $Path)
            {

                #Loading the file
                $xml = New-Object -TypeName XML
                $xml.Load($Path)

                # Getting all the values
                $Items  = Select-Xml -Path $Path -XPath $xPath
                ForEach ($Item in $Items) 
                {   
                    $Item.Node.$attribute
                    If ($($Item.Node.$attribute).Contains($valueToFind) ){
                        "Before Replace the value $($Item.Node.$attribute)"

                        $Item.Node.$attribute = $($Item.Node.$attribute) -Replace "$valueToFind","$ValueToReplace"


                    }
                    Else {

                        Write-Error "$valueToFind not available"

                    }
                }
                "After replace $($Item.Node.$attribute)"
                "Saving the $Path"
                 $xml.Save((Resolve-Path "$Path"))
            }
            Else 
            {
                Write-Error "$Path is not found"
            }
        }
        catch 
        {

            $_.Exception.Message
            $_.Exception.ItemName
            Write-Error "Set-XMLAttribute function failed."
        }
    }
    End {}
} # End Function Update-XMLAttribute
Set-XMLAttribute -Path "E:\Pshscript\web.Config" -xPath "/configuration/system.serviceModel/behaviors/serviceBehaviors/behavior/serviceMetadata" -attribute "externalMetadataLocation" -valueToFind "http" -ValueToReplace "https"
2
  • Do you open PS console as admin? Write rights on the file correct? Commented Apr 28, 2015 at 12:41
  • @EduardUta Yes it is open as admin Commented Apr 28, 2015 at 13:09

1 Answer 1

3

The reason you're seeing this is that you're making changes to $XML, which is an object in memory. You have to save these settings to a file to reflect it there, so you do that by calling the .Save() method of $XML, which you're doing. I think the problem here is that you're trying to save with the following line:

$xml.Save((Resolve-Path "$Path"))

I believe this syntax is causing the problem, as Resolve-Path returns an object with a Path property. Try this instead.

$xml.Save((Resolve-Path "$Path").Path)
Sign up to request clarification or add additional context in comments.

8 Comments

Isnt that what was done with ops code here $xml.Save((Resolve-Path "$Path"))?
@FoxDeploy I have done " $xml.Save((Resolve-Path "$Path"))" . What is wrong with that?
Ah, I re-read your code and agree with you. I've edited my answer, please give this a try.
@FoxDeploy I tried your latest updates but still it is not working. Totally strange
Hey man, can you post one of these files (the webconfig files) on pastebin for me to troubleshoot this further? I'm having a very hard time replicating what should be happening here without the same sort of file.
|

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.