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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10854009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:30:19  来源:igfitidea点击:

Can't extract value of XML nodes with powershell

xmlpowershell

提问by Micah

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 文件中提取一些信息并根据需要更新/创建应用程序池。这是我正在阅读的 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>

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
}

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

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

回答by Trevor Sullivan

Corrected answer:

更正的答案:

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

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

Consider this also, which uses the InnerTextproperty on the System.Xml.XmlElementobjects:

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

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

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