【问题标题】:How to update a string in the [xml] attribute using powershell?如何使用 powershell 更新 [xml] 属性中的字符串?
【发布时间】:2014-11-17 16:42:37
【问题描述】:

我有许多 web.config 文件。其中 URL 包含机器名称。

这是我的示例 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>

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

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

    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"

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

【问题讨论】:

    标签: xml powershell powershell-2.0


    【解决方案1】:

    您的替换值不会保存回节点上。更改此行:

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

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

    【讨论】:

      【解决方案2】:

      如果只是为了替换你上面提到的XML结构中的机器名,你真的不需要解析XML并使用XPath...

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • :我也添加了保存功能但是还是没用($WebConfig.Save($FilePath))
        【解决方案4】:

        你可以这样使用:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-01-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-01
          相关资源
          最近更新 更多