无法使用PowerShell提取XML节点的值

Mic*_*cah 18 xml powershell

我正在尝试从xml文件中提取一些信息,并根据需要更新/创建应用程序池.这是我正在阅读的xml:

<?xml version="1.0" encoding="utf-8"?>
<appPool name="MyAppPool">
  <enable32BitAppOnWin64>true</enable32BitAppOnWin64>
  <managedPipelineMode>Integrated</managedPipelineMode>
  <managedRuntimeVersion>v4.0</managedRuntimeVersion>
  <autoStart>true</autoStart>
</appPool>
Run Code Online (Sandbox Code Playgroud)

这是我正在尝试用它做的事情:

#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
}
Run Code Online (Sandbox Code Playgroud)

$_.Name返回节点的名称,(即enable32BitAppOnWin64)是正确的,但.Value属性不返回任何内容.如何提取我想要的数据?

Tre*_*van 24

更正的答案:

你想要$_.'#text'而不是$_.Value.

还要考虑这个,它使用对象InnerText上的属性System.Xml.XmlElement:

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

  • 请注意,InnerText获取节点下的所有文本,例如([xml]"<xml> foo <bar> bar </ bar> </ xml>").xml.InnerText返回"foobar",而您可能想要"富".(......).xml.'#text'返回"foo". (5认同)
  • 实际上正确的PowerShell应该是`$ appPoolXml.appPool.ChildNodes | %{$ _.姓名; $ _.InnerText}` (4认同)