Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell-如何添加<;部门组>;到web.config_Powershell_Web Config_Configsection - Fatal编程技术网

Powershell-如何添加<;部门组>;到web.config

Powershell-如何添加<;部门组>;到web.config,powershell,web-config,configsection,Powershell,Web Config,Configsection,我正在尝试使用Powershell向web.config中的configuration/configSections元素添加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 <configSect

我正在尝试使用Powershell向web.config中的
configuration/configSections
元素添加
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)
但这会引发与之前完全相同的异常

绝对是
的有效子级

理想情况下,我更希望第二次尝试成功,因为这不需要我声明每个元素、每个属性等

有人能给我解释一下为什么
节点不允许我的
元素吗?

这应该可以做到:

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

不,没有。在“CreateElement”调用中,它抛出异常“指定的节点不能作为此节点的有效子节点插入,因为指定的节点是错误的类型。”这是一个遗憾,而且奇怪的是,我的答案中的代码对我来说非常适合,我无法让它抛出任何异常。我想知道它是否与环境有关?我正在为NuGet软件包构建脚本,但目前正在PowerGUI中使用硬编码文件路径对其进行测试。我已临时将我的计算机上的ExecutionPolicy更改为“Unrestricted”,并且web.config文件在其他任何位置都未打开(尽管其父项目在VS中打开)。有什么听起来可能是问题的原因吗?有可能,但排除问题的方法是从powershell命令行运行它。现在,通过将其保存为.ps1文件并运行该文件,它可以正常工作-它仍然会在PowerGUI中引发异常。现在我已经运行了它(作为一个文件),我想在
myCustomSectionGroup
中添加子节点,但是当我第二次调用
CreateElement
时,它会抛出相同的异常。我第二次调用
$xml.CreateElement(“section”)
是对的,还是应该是
$sectionGroup.CreateElement(“section”)
?[两个都会引发异常。]另外,我不需要在创建文件后立即保存该文件或追加
myCustomGroup
,对吗?我可以添加所有内容,然后追加并保存?完整堆栈跟踪将非常有用。目前,您的代码完全正常。它在命令行中运行,在PowerGui中运行
CreateElement
不太可能产生您正在描述的异常。我认为你正在做的事情你没有告诉我们。因此,请在您描述的异常发生后立即读取
$error[0].Exception.InnerException.StackTrace
来获取堆栈跟踪。如果我使用第二个示例中的脚本,然后请求StackTrace,则异常为“at System.Xml.XmlNode.AppendChild(XmlNode newChild at AppendChild(Object,Object[])在System.Management.Automation.DotNetAdapter.Auxi-tion-methodInformation,Object[]originalArguments)“我仍然更喜欢使用上面的版本,该版本将XML(要插入)作为XML文档,然后再追加,而不是逐个属性(如下面的答案所示)逐个元素地写出所有内容。将XML声明为变量似乎更易于维护和可读,因此如果您能指出此方法可能存在的问题,我将不胜感激。是的,堆栈跟踪表明失败的不是
CreateElement
,而是
AppendChild
DotNetAdapter
行表示您正在从powershell代码调用此dotnet方法。(确实如此)在调用
AppendChild
之前尝试转储方法参数,它们必须具有我们不期望的内容。某些节点类型无法添加到其他节点类型,并且(例如无法将元素添加到属性)像这样的不兼容是导致异常的最终原因。您希望在正在调用的对象上转储产生错误的
AppendChild
,以及要传递给
AppendChild
的对象上的
NodeType
属性。
$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)