[英]Adding New ChildNodes in XML WIth PowerShell
我支持基于Web的应用程序,并且在升级到最新版本时,有些配置无法正确更新。 此问题将在下一发行版中修复,但与此同时,我们需要向少数XML文件中添加一些信息。 为了消除人为错误的机会,我希望为此编写一个Powershell脚本,但是遇到了一些麻烦...
因此,我有一个包含以下信息的XML文件:
<configuration>
<configSections>
<appSettings>
<system.web>
<system.webServer>
<httpRedirect enabled="false" />
<security>
<authentication>
<anonymousAuthentication enabled="true" />
<windowsAuthentication enabled="false" />
</authentication>
</security>
<httpProtocol>
<customHeaders>
<add name="HeaderName" value="HeaderValue" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
我需要在system.webServer部分的顶部添加以下信息:
<handlers>
<add name="NAME1" preCondition="Precondition" type="Type" path="Path" verb="*"/>
<add name="NAME2" preCondition="Precondition" type="Type" path="Path" verb="*"/>
</handlers>
到目前为止,我得到的是:
#Declare variable for file path
$Path = "C:\Folder\File.xml"
#Declare variable to view path as XML
[XML]$File = gc $Path
#Variable for Node
$Node = $File.configuration."system.webServer"
$Variable for newNode
$newNode = @"
<handlers>
<add name="NAME1" preCondition="Precondition" type="Type" path="Path" verb="*"/>
<add name="NAME2" preCondition="Precondition" type="Type" path="Path" verb="*"/>
</handlers>
"@
#Add $newNode to system.webServer section of File.xml file just before httpRedirect section
$Node.InsertBefore($File.ImportNode($newNode.handlers, $true), $Node.httpRedirect) | out-Null
#Save the file
$File.Save($Path)
但是当我运行它时,我得到了错误:
Exception calling "ImportNode" with "2" argument(s): "Cannot import a null node."
我正在努力弄清自己可能做错了什么,并且好奇是否有人可以指出正确的方向。 过去,我做了一些XML操作,但这主要是在更改值。 我以前从未添加过XML文件。
$newNode.handlers
似乎没有引用任何内容,在此基础上,错误提示它正在尝试导入空节点。
$newNode
似乎存储为字符串,并且没有“ handlers”成员属性。
所以我需要改变
$newNode = @"
<handlers>
<add name="NAME1" preCondition="Precondition" type="Type" path="Path" verb="*"/>
<add name="NAME2" preCondition="Precondition" type="Type" path="Path" verb="*"/>
</handlers>
"@
至
$newNode = [XML]@"
<handlers>
<add name="NAME1" preCondition="Precondition" type="Type" path="Path" verb="*"/>
<add name="NAME2" preCondition="Precondition" type="Type" path="Path" verb="*"/>
</handlers>
"@
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.