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") {
$WebConfig = [XML] (Get-content $Filepath)
$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
source
share