How to update string in attribute [xml] using powershell?

I have many web.config files. The URL contains the name of the machine.

This is my example web.config file

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.Model>
<client>
      <endpoint address="http://MyMachineName:80/MyProj/Core/NewService/SecurityObject.0/Secretservice.svc" binding="basicHttpBinding" bindingConfiguration="MyProj_Internal_Routing" behaviorConfiguration="UserSIDBehavior" contract="SecurityMgmt.SecurityMgmtContract" name="HttpEndpointSecMgt" />
      <endpoint address="http://MyMachineName:80/MyProj/Core/DataObject/SystemCatalogs1.0/DataObjectservice/DataObjectservice.svc" binding="basicHttpBinding" bindingConfiguration="MyProj_Internal_Routing" behaviorConfiguration="UserSIDBehavior" contract="DataObjectservice.SystemCatalogsDataObjectservice" name="BasicHttpBinding_SystemCatalogsDataObjectservice" />
      <endpoint address="http://MyMachineName:80/MyProj/Core/NewService/Movement.0/Movement.svc" binding="basicHttpBinding" bindingConfiguration="MyProj_Internal_Routing" behaviorConfiguration="UserSIDBehavior" contract="NavigationClientLib.NavigationNewService" name="BasicHttpBinding_NavigationNewService" />
 </client>

 <system.Model>

 </configuration>

When the user changes the name of the machine, this should be updated in all xml files.

I decided to write such a function.

    Function Update-MachineName ([string] $FilePath,[string] $xpath, [string] $NewMachineName="NewComputer") {


    # Get the config file contents
    $WebConfig = [XML] (Get-content $Filepath)

    #Find the url using xpath which has old Machine name 
    $hostnameString= Select-XML -XML $WebConfig -XPath $Xpath

    Foreach ( $hostname in $hostnameString) { 

           if ( ($hostname.Node.value -match 'http') -eq $true ) {

               $hostname.Node.value
               $MachineOldName = $($($hostname.Node.value.split("/"))[2].split(":"))[0]
               $MachineOldName

               $hostname.Node.value.replace( $MachineOldName,$NewMachineName)

           }
    }

 $WebConfig.Save($FilePath)
}

Update-MachineName "C:\web.config" "//@address"

Although I am replacing the new machine name, the contents of web.config are not updated. How to update it with the new name Machine

+3
source share
4 answers

Your replacement value is not stored on node. Change this line:

$hostname.Node.value.replace( $MachineOldName,$NewMachineName)

For

$hostname.Node.value=$hostname.Node.value.replace( $MachineOldName,$NewMachineName)
+3
source

If this is just to replace the machine names in the XML structure that you talked about above, you don't need to parse the XML and use XPath ...

$oldMachineName = "MyMachineName"
$newMachineName = "MyBrandNewMachineNAme"

$content = Get-Content -path $filePath
$content | foreach { $_.Replace($oldMachineName, $newMachineName) } | Set-Content $filePath
+3
source

. xml , , .

+1

:

$nodoEndpointSvcAddress = $($configXml.configuration.'system.serviceModel'.client.endpoint |  where { $_.name -eq "MyEndPoint1" })
[string]$endpointSvcAddress = $nodoEndpointSvcAddress.address
Write-Host $endpointSvcAddress

# Replace
$server = "test"
iif ($endpointSvcAddress.Contains($server))
{
    Write-Host "`r`nReplace $endpointSvcAddress"
    $nodoEndpointSvcAddress.address=$nodoEndpointSvcAddress.address.replace( $server,"PROSERVER")
    Write-Host ("Replaced " + $nodoEndpointSvcAddress.address)

}

$configXml.Save($pathCDR)
0

All Articles