简体   繁体   English

使用PowerShell在XML中添加新的ChildNode

[英]Adding New ChildNodes in XML WIth PowerShell

I support a web based application, and when upgrading to the latest version there are some configurations that do not update properly. 我支持基于Web的应用程序,并且在升级到最新版本时,有些配置无法正确更新。 This is being fixed in the next release, but in the meantime we need to add some information to a handful of XML files. 此问题将在下一发行版中修复,但与此同时,我们需要向少数XML文件中添加一些信息。 In order to eliminate the chance for human error, I was hoping to write a Powershell script for this, but I'm having some trouble... 为了消除人为错误的机会,我希望为此编写一个Powershell脚本,但是遇到了一些麻烦...

So I have an XML file with the following information: 因此,我有一个包含以下信息的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>

I need to add the following information to the top of the system.webServer section: 我需要在system.webServer部分的顶部添加以下信息:

<handlers>
    <add name="NAME1" preCondition="Precondition" type="Type" path="Path" verb="*"/>
    <add name="NAME2" preCondition="Precondition" type="Type" path="Path" verb="*"/>
</handlers>

What I've got so far is: 到目前为止,我得到的是:

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

But when I run it I get the error: 但是当我运行它时,我得到了错误:

Exception calling "ImportNode" with "2" argument(s): "Cannot import a null node."

I'm struggling to figure out what I may have done wrong and am curious if anyone might be able to point me in the right direction. 我正在努力弄清自己可能做错了什么,并且好奇是否有人可以指出正确的方向。 I've done a bit of XML manipulation in the past, but it was mostly changing values. 过去,我做了一些XML操作,但这主要是在更改值。 I have never added to an XML file before. 我以前从未添加过XML文件。

$newNode.handlers doesn't seem to refer to anything, the error saying it's trying to import a null node seems correct on this basis. $newNode.handlers似乎没有引用任何内容,在此基础上,错误提示它正在尝试导入空节点。

$newNode appears to be stored as a string and doesn't have a "handlers" member property. $newNode似乎存储为字符串,并且没有“ handlers”成员属性。

So I needed to change 所以我需要改变

$newNode = @"
    <handlers>
        <add name="NAME1" preCondition="Precondition" type="Type" path="Path" verb="*"/>
        <add name="NAME2" preCondition="Precondition" type="Type" path="Path" verb="*"/>
    </handlers>
"@

to

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM