2013-06-16 3 views
2

Powershell을 사용하여 web.config의 configuration/configSections 요소에 sectionGroup 요소를 추가하려고합니다.Powershell - web.config에 <sectionGroup>을 추가하는 방법

나는 현재

$filePath = [path to my web.config file] 

# load the XML from the web.config 
$xml = New-Object XML 
$xml = [xml](Get-Content $filePath) 

# navigate to the <configSections> element 
$xmlConfigSections = $xml.SelectSingleNode("//configuration/configSections") 

# create the new <sectionGroup> element with a 'name' attribute 
$sectionGroup = $xml.CreateElement("sectionGroup") 
$xmlAttr = $xml.CreateAttribute("name") 
$xmlAttr.Value = "myCustomSectionGroup" 
$sectionGroup.Attributes.Append($xmlAttr) 

# now add the new <sectionGroup> element to the <configSections> element 
$xmlConfigSections.AppendChild($sectionGroup) 

#save the web.config 
$xml.Save($filePath) 

해야하지만이 CreateElement 방법에 예외 발생시킨다 :

"지정된 때문에 지정된 노드가이 노드의 유효한 자식으로 삽입 할 수 없습니다 노드를 잘못된 유형입니다. " 이러한 예외가 발생하는 이유 나는 (예외가 요소를 추가 관련이 보인다)에게 요소를 생성 하려고 할 때

는 이해가 안 돼요. 내가 해봤 다른

뭔가

$newConfig = [xml]@'<sectionGroup name="myCustomSectionGroup"></sectionGroup>'@ 

$filePath = [path to my web.config file] 

# load the XML from the web.config 
$xml = New-Object XML 
$xml = [xml](Get-Content $filePath) 

# navigate to the <configSections> element 
$xmlConfigSections = $xml.SelectSingleNode("//configuration/configSections") 

$xmlConfigSections.AppendChild($newConfig) 

이다 그러나 이것은 이전과 같이 동일한 예외가 발생합니다.

<sectionGroup>은 확실히 <configSections>의 유효한 어린이입니다.

모든 요소, 모든 특성, 을 선언 할 필요가 없기 때문에 두 번째 시도가 효과적이면 이상적입니다.

누군가 <configSections> 노드가 내 <sectionGroup> 요소를 허용하지 않는 이유를 설명해 줄 수 있습니까?

+0

전체 스택 추적은 많은 도움이 될 것입니다. 현재 귀하의 코드는 완벽합니다. PowerGui에서 실행되는 명령 줄에서 실행됩니다. 'CreateElement'는 당신이 설명하는 예외를 생성하지 않을 것입니다. 나는 당신이 우리에게 말하지 않는 것을하고 있다고 생각합니다. 그러므로, 당신이 파기하고있는 예외가 발생한 직후에'$ error [0] .Exception.InnerException.StackTrace'를 읽어 스택 추적을 얻으십시오. –

+0

두 번째 예제에서 스크립트를 사용한 다음 StackTrace를 요청하면 "System.Xml.XmlNode.AppendChild (XmlNode.ChildChild (XmlNode newChild) at AppendChild (Object, Object []) at System.Management.Automation.DotNetAdapter .Auxi 메소드 methodInformation, Object [] originalArguments) " – awj

+0

XML을 삽입 할 XML이있는 요소를 요소 대신 요소를 모두 쓰는 대신 추가하는 XML 문서로 사용하는 것을 선호합니다. 속성에 의한 속성 (아래 답변에서와 같이). 변수로 선언 된 XML을 유지 관리하기 쉽고 읽을 수있는 것처럼 보이기 때문에이 방법으로 문제가 될 것이라고 생각하는 점을 지적 해 주시면 감사하겠습니다. – awj

답변

6

이 그것을 수행해야합니다

$filePath = [path to my web.config file] 

# load the XML from the web.config 
$xml = New-Object XML 
$xml = [xml](Get-Content $filePath) 

$sectionGroup = $xml.CreateElement('sectionGroup') 
$sectionGroup.SetAttribute('name','myCustomSectionGroup') 
$sectionGroupChild = $xml.CreateElement('sectionGroupChild') 
$sectionGroupChild.SetAttribute('name','myCustomSectionGroup') 

$newNode = $xml.configuration.configSections.AppendChild($sectionGroup) 
$newNode.AppendChild($sectionGroupChild) 

$xml.Save($filePath) 
+0

아니, 그렇지 않습니다. "CreateElement"호출에서 "지정된 노드가 잘못된 유형이기 때문에 지정된 노드를 이 노드의 유효한 하위 노드로 삽입 할 수 없습니다."예외가 발생합니다. – awj

+0

그건 수치스럽고 이상합니다. 제 답변의 코드가 완벽하게 작동하고 예외를 발생시킬 수 없습니다. –

+0

환경과 관련이 있는지 궁금합니다. NuGet 패키지 용 스크립트를 작성하고 있지만 현재 PowerGUI에서 하드 코드 된 파일 경로로 테스트 중입니다. 임시로 내 컴퓨터의 ExecutionPolicy를 'Unrestricted'로 변경했으며 web.config 파일은 다른 곳에서는 열지 않았습니다 (상위 프로젝트는 VS에서 열림). 문제의 원인은 무엇입니까? – awj