Share via


Powershell script to Edit an XML file

Question

Monday, March 27, 2017 2:10 AM

Hi All - Need some help.. I have a xml file called abc.xml. In this abc.xml, I have a tag by name: urn:jboss:domain:security:1.2 with the below Attributes:

    <subsystem xmlns="urn:jboss:domain:security:1.2">
      <security-domains>
        <security-domain name="other" cache-type="default">
          <authentication>
            <login-module code="Remoting" flag="optional">
              <module-option name="password-stacking" value="useFirstPass" />
            </login-module>
            <login-module code="RealmDirect" flag="required">
              <module-option name="password-stacking" value="useFirstPass" />
            </login-module>
          </authentication>
        </security-domain>
        <security-domain name="jboss-web-policy" cache-type="default">
          <authorization>
            <policy-module code="Delegating" flag="required" />
          </authorization>
        </security-domain>
        <security-domain name="jboss-ejb-policy" cache-type="default">
          <authorization>
            <policy-module code="Delegating" flag="required" />
          </authorization>
        </security-domain>
      </security-domains>
    </subsystem>

I would like to add new attribute to the same tag: Below is the new attribute. [Highlighted]

    <subsystem xmlns="urn:jboss:domain:security:1.2">
      <security-domains>
        <security-domain name="other" cache-type="default">
          <authentication>
            <login-module code="Remoting" flag="optional">
              <module-option name="password-stacking" value="useFirstPass" />
            </login-module>
            <login-module code="RealmDirect" flag="required">
              <module-option name="password-stacking" value="useFirstPass" />
            </login-module>
          </authentication>
        </security-domain>
        <security-domain name="jboss-web-policy" cache-type="default">
          <authorization>
            <policy-module code="Delegating" flag="required" />
          </authorization>
        </security-domain>
        <security-domain name="jboss-ejb-policy" cache-type="default">
          <authorization>
            <policy-module code="Delegating" flag="required" />
          </authorization>
        </security-domain>
<security-domain name="ABCD" cache-type="default">
***    <authentication>***
***     <login-module code="org.1234.identity.federation.bindings.jboss.auth.ABCD2LoginModule" flag="required"/>***
***      </authentication>***
***     </security-domain>***
      </security-domains>
    </subsystem>

Can you please help me in getting a powershell script to update the abc.xml. with new values.

Thanks in advance.

All replies (4)

Monday, March 27, 2017 3:49 AM ✅Answered | 2 votes

[xml]$xml = Get-Content abc.xml
$nsmgr = [System.Xml.XmlNamespaceManager]::new($xml.NameTable)
$nsmgr.AddNamespace('x', "urn:jboss:domain:security:1.2")

$xmlchild = $xml.CreateDocumentFragment()
$xmlchild.InnerXml = $fragment

$securitydomains = $xml.SelectSingleNode('//x:security-domains',$nsmgr)
$node = $securitydomains.AppendChild($xmlchild)

# fix bug
$ns = $node.Attributes.GetNamedItem('xmlns')
$node.Attributes.Remove($ns)

$xml.Save("$pwd\abc.xml")
. "$pwd\abc.xml"

\(ツ)_/


Monday, March 27, 2017 2:26 AM

What did you try so far? Do you already have some code to show? If not - I recommend you read the free Powershell cook book XML. That will help you find a starting point. If you have some issues then with your code you can show it here and we will try to help you. But we will not write scripts for you.

If you want you can search for already written scripts in the Microsoft Technet Script Gallery. I'm pretty sure there wíll be something you could adapt to your special needs.

Good luck!

Grüße - Best regards

PS:> (79,108,97,102|%{[char]$_})-join''


Monday, March 27, 2017 2:37 AM

You can load the xml this way:

Select-Xml abc.xml -Xpath '//x:security-domain[@name="ABCD"]' -Namespace @{x="urn:jboss:domain:security:1.2"}

Next you will need to learn how to use the XML DOM to create and add the needed biys:

Start here http://www.w3schools.com/xml

\(ツ)_/


Monday, March 27, 2017 7:59 PM

Thanks..Worked well :)