如何使用powershell更新[xml]属性中的字符串?

时间:2022-07-30 21:45:59

I am having numerous web.config files. In which URL contains machine name.

我有很多web.config文件。在哪个URL包含计算机名称。

This is my sample web.config file

这是我的示例web.config文件

<?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 user changes machine name this has to be updated in all the xml files.

当用户更改计算机名称时,必须在所有xml文件中更新。

I decided to write a function like this.

我决定写一个这样的函数。

    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"

Though i replace with new machine name, still the web.config content doesn't updated. How to make it updated with the new Machine name

虽然我用新的机器名替换,但web.config内容仍未更新。如何使用新计算机名更新它

4 个解决方案

#1


3  

Your replacement value is not saved back on the node. Change this line:

您的替换值不会保存在节点上。改变这一行:

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

To

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

#2


3  

If it's only for replacing machine names in the XML structure you mentioned above, you don't really need to parse XML and use XPath...

如果它仅用于替换上面提到的XML结构中的机器名称,那么您实际上不需要解析XML并使用XPath ......

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

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

#3


1  

In your function, you must save the file back to the disk. You are manipulating with xml in memory, so changes will be visible only in the console, not in the actual file.

在您的函数中,您必须将文件保存回磁盘。您正在使用内存中的xml进行操作,因此更改只能在控制台中显示,而不能在实际文件中显示。

#4


0  

You can use like this:

你可以像这样使用:

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

#1


3  

Your replacement value is not saved back on the node. Change this line:

您的替换值不会保存在节点上。改变这一行:

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

To

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

#2


3  

If it's only for replacing machine names in the XML structure you mentioned above, you don't really need to parse XML and use XPath...

如果它仅用于替换上面提到的XML结构中的机器名称,那么您实际上不需要解析XML并使用XPath ......

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

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

#3


1  

In your function, you must save the file back to the disk. You are manipulating with xml in memory, so changes will be visible only in the console, not in the actual file.

在您的函数中,您必须将文件保存回磁盘。您正在使用内存中的xml进行操作,因此更改只能在控制台中显示,而不能在实际文件中显示。

#4


0  

You can use like this:

你可以像这样使用:

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