2009-08-25 3 views
2

web.config의 appSettings 요소에 대한 configSource를 업데이트하여 다른 환경에 대한 web.config를 업데이트하는 방법을 찾으려고합니다.매개 변수를 전달하여 Powershell을 사용하여 web.config의 XML 요소의 configSource를 업데이트하십시오.

내가 어떻게하는지 알고있는 방법입니다.

$xml.get_DocumentElement().appSettings.configSource = $replaced_test 

문제

내가 내가 내가 변경하려면 스크립트 및 업데이트에 서로 다른 노드에 전달할 수있는 하나의 기본 스크립트를 원하지만 내가 그것을 할 방법을 잘 모르겠습니다이다.

예를 들어, 나는이 충분히 분명 희망이

changeWebConfig.ps1 nodeToChange newValueofNode 

같은 PowerShell 스크립트를 호출 할 수 있어야합니다.

이것은 내가 지금 가지고있는 코드입니다.

$webConfigPath = "C:\web.config" 

# Get the content of the config file and cast it to XML 
$xml = [xml](get-content $webConfigPath) 

#this was the trick I had been looking for 
$root = $xml.get_DocumentElement()."system.serviceModel".client.configSource = $replace 

# Save it 
$xml.Save($webConfigPath) 

내가

<configuration> 

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> 

이에서를 변경 한 구성 노드

을했다 가졌다 문제 어떻게 확실하지 않다 구성을 가진 노드를 찾으려면 이온 노드는 아직 원래 상태입니다.하지만 점점 가까워지고 있습니다.

function Set-ConfigAppSetting 
([string]$PathToConfig=$(throw 'Configuration file is required'), 
     [string]$Key = $(throw 'No Key Specified'), 
     [string]$Value = $(throw 'No Value Specified')) 
{ 
    if (Test-Path $PathToConfig) 
    { 
     $x = [xml] (type $PathToConfig) 
     $node = $x.SelectSingleNode("//client[@configSource]") 
     $node.configSource = $Value 
     $x.Save($PathToConfig) 
    } 
} 

set-configappsetting "c:\web.config" CurrentTaxYear ".\private$\dinnernoworders" -confirm 

답변

3

마지막으로 알아 냈습니다. 물론

$root = $xml.get_DocumentElement().SelectSingleNode("//client[@configSource]").configSource = "test" 

, 내가 대체 할 것이다 "// 클라이언트는 [@configSource]"변수로 그래서 난 내 기본 스크립트를 만들 수있는 매개 변수로 서로 다른 노드에 전달할 수 있습니다.

+0

참고하면 get_DocumentElement를 사용할 필요가 없습니다) (: 여기

노드가 뭐죠 볼 수있는 방법입니다. $ xml.SelectSingleNode ("...")는 SelectSingleNode가 XmlDocument에서도 사용 가능하며,이 경우 $ xml이 사용 가능합니다. –

2

나는 코드를 수정할 수있는 방법을 찾고 있습니다.

$path = 'c:\site\web.config' 
$PublishState = (Select-Xml -Path $path -XPath "configuration/appSettings/add[@key='PublishState']/@value").Node.'#text' 
$PublishState 
관련 문제