XML in Powershell - beginning

XML in Powershell for beginners :)

I have the following XML structure:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<roles version="2.0">
	<role id="7722" name="System1">
		<system permission="allow"/>
		<folders permission="allow"/>
		<globalfavorites/>
		<user>joe</user>
	</role>
</roles>

Open in new window


(read XML)

How do I add a role? (including childs)

How do I delete a role? (including childs)

(and save to file)


Thanks in advance

Michael
LVL 1
mikeydkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Define some xml:
# can as well be done with 
# $xml = [xml](Get-Content -Path C:\Temp\Whatever.xml)
$xml = [xml]@'
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<roles version="2.0">
	<role id="7722" name="System1">
		<system permission="allow"/>
		<folders permission="allow"/>
		<globalfavorites/>
		<user>joe</user>
	</role>
</roles>
'@

Open in new window

You add a node by first creating a new element:
$element = $xml.CreateElement('role')

Open in new window

Then you select the node where you want to add the child (using an XPath, https://www.w3schools.com/xml/xpath_intro.asp), and append the new element:
$roles = $xml.SelectSingleNode('roles')
$newRole = $roles.AppendChild($element)

Open in new window

Finally you add the attributes you want the node to have:
$newRole.SetAttribute('id', 42)
$newRole.SetAttribute('name', 'System2')

Open in new window

The children are added just the same; here as one-liners.
$newRole is assumed to be still set to the new node created in the step before:
$system = $newRole.AppendChild($xml.CreateElement('system'))
$system.SetAttribute('permission', 'allow')
$folders = $newRole.AppendChild($xml.CreateElement('folders'))
$folders.SetAttribute('permission', 'allow')
$globalfavorites = $newRole.AppendChild($xml.CreateElement('globalfavorites'))

Open in new window

For the "user" node, you need to set the node's "InnerText":
$user = $newRole.AppendChild($xml.CreateElement('user'))
$user.InnerText = 'joe'

Open in new window


Deleting a node is similar; you select the parent node (assumed to still be set in $roles from the previous command), you select the node to delete (here for example by the "id" attribute), and call the parent's RemoveChild() method:
$role42 = $xml.SelectSingleNode("roles/role[@id='42']")
$deletedRole = $roles.RemoveChild($role42)

Open in new window

To save, you just call the xml object's Save() method:
$xml.Save('C:\Temp\Whatever.ps1')

Open in new window


Note that in XML, node and attribute names are case sensitive, and so are the comparisons in an XPath. There are ways around that in XPath directly, but it can be easier to do case insensitive filtering in PowerShell. For example, if you want to filter by the name attribute, where it could be system1 or System1:
$system1 = $xml.SelectNodes('roles/role') | Where-Object {$_.GetAttribute('name') -eq 'system1'}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.