简体   繁体   中英

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. This is being fixed in the next release, but in the meantime we need to add some information to a handful of XML files. 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...

So I have an XML file with the following information:

<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:

<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. I have never added to an XML file before.

$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 appears to be stored as a string and doesn't have a "handlers" member property.

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>
"@

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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