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>
요소를 허용하지 않는 이유를 설명해 줄 수 있습니까?
전체 스택 추적은 많은 도움이 될 것입니다. 현재 귀하의 코드는 완벽합니다. PowerGui에서 실행되는 명령 줄에서 실행됩니다. 'CreateElement'는 당신이 설명하는 예외를 생성하지 않을 것입니다. 나는 당신이 우리에게 말하지 않는 것을하고 있다고 생각합니다. 그러므로, 당신이 파기하고있는 예외가 발생한 직후에'$ error [0] .Exception.InnerException.StackTrace'를 읽어 스택 추적을 얻으십시오. –
두 번째 예제에서 스크립트를 사용한 다음 StackTrace를 요청하면 "System.Xml.XmlNode.AppendChild (XmlNode.ChildChild (XmlNode newChild) at AppendChild (Object, Object []) at System.Management.Automation.DotNetAdapter .Auxi 메소드 methodInformation, Object [] originalArguments) " – awj
XML을 삽입 할 XML이있는 요소를 요소 대신 요소를 모두 쓰는 대신 추가하는 XML 문서로 사용하는 것을 선호합니다. 속성에 의한 속성 (아래 답변에서와 같이). 변수로 선언 된 XML을 유지 관리하기 쉽고 읽을 수있는 것처럼 보이기 때문에이 방법으로 문제가 될 것이라고 생각하는 점을 지적 해 주시면 감사하겠습니다. – awj