2012-05-14 2 views
3

수많은 web.config 파일이 있습니다. 어떤 URL에 컴퓨터 이름이 포함되어 있습니다.powershell을 사용하여 [xml] 속성의 문자열을 업데이트하는 방법은 무엇입니까?

이 내 샘플 web.config 파일

사용자가 컴퓨터 이름이 모든 XML 파일에 업데이트해야하는 변경
<?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> 

입니다.

나는 이와 같은 함수를 작성하기로 결정했습니다.

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

1

함수에서 파일을 디스크에 다시 저장해야합니다. 메모리의 xml로 조작하고 있으므로 변경 내용은 실제 파일이 아닌 콘솔에서만 볼 수 있습니다. 귀하의 대체 값이 다시 저장되지 않습니다

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

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

: 나는 또한 기능을하지만 여전히 아무 소용 저장 추가 ($ WebConfig.Save ($ FilePath를

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

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

3

단지 당신이 위에서 언급 한 XML 구조에서 컴퓨터 이름을 교체하기위한, 당신이 정말로 XML을 구문 분석하고 XPath를 사용할 필요가 없습니다 있다면 ... 마디.

$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

당신은 다음과 같이 사용할 수 있습니다

하려면이 줄을 변경))
관련 문제