2012-05-14 72 views
3

我有很多web.config文件。哪個URL包含機器名稱。如何使用PowerShell更新[xml]屬性中的字符串?

這是我的樣本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內容仍未更新。如何使用新機器名更新

回答

3

你的重置價值沒有救回來的節點。改變這一行:

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

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

在您的功能中,您必須將文件保存回磁盤。您在內存中使用xml進行操作,因此更改只能在控制檯中顯示,而不在實際文件中顯示。

+0

:我已經加入保存功能也,但仍然沒有使用($ WebConfig.Save($文件路徑)) – Samselvaprabu

3

如果它只是在你上面提到的XML結構代替機器名,你並不真的需要解析XML和使用XPath ...

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

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

你可以使用這樣的:

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