19

I'm trying to extract some info from an xml file and update/create an app pool as needed. Here's the xml I'm reading in:

<?xml version="1.0" encoding="utf-8"?>
<appPool name="MyAppPool">
  <enable32BitAppOnWin64>true</enable32BitAppOnWin64>
  <managedPipelineMode>Integrated</managedPipelineMode>
  <managedRuntimeVersion>v4.0</managedRuntimeVersion>
  <autoStart>true</autoStart>
</appPool>

Here's what I'm trying to do with it:

#read in the xml
$appPoolXml = [xml](Get-Content $file)

#get the name of the app pool
$name = $appPoolXml.appPool.name

#iterate over the nodes and update the app pool
$appPoolXml.appPool.ChildNodes | % {
     #this is where the problem exists
     set-itemproperty IIS:\AppPools\$name -name $_.Name -value $_.Value
}

$_.Name returns the name of the node, (i.e. enable32BitAppOnWin64) which is correct, but the .Value property doesn't return anything. How do I extract the data I want?

1 Answer 1

25

Corrected answer:

You want $_.'#text' instead of $_.Value.

Consider this also, which uses the InnerText property on the System.Xml.XmlElement objects:

$xml.appPool.ChildNodes | % { $.Name; $.InnerText };

Sign up to request clarification or add additional context in comments.

2 Comments

Actually the correct PowerShell should read $appPoolXml.appPool.ChildNodes | % { $_.Name; $_.InnerText }
Note that InnerText gets all the text under a node, such that ([xml]"<xml>foo<bar>bar</bar></xml>").xml.InnerText returns "foobar", whereas you probably want "foo".(...). xml.'#text' returns "foo".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.