PowerShell Get-Content修剪线

时间:2016-02-18 21:45:00

标签: powershell

我是PowerShell的新手。我正在尝试解析XML并将属性值存储在变量中。目标是能够获取块之间的值并存储在var中。我可以做到,但我正在存储整条线。我不知道如何修剪它只是值。

XML格式代码段

<Processors>
  <Processor>
    <Manufacturer>Intel(R) Corporation</Manufacturer>
    <Version>Intel(R) Xeon(R) CPU E5-2609 v3 @ 1.90GHz</Version>
    <Speed>1900MHz</Speed>
  </Processor>
  <Number>1</Number>
$var = (Get-Content) discovery.xml | Where-Object {$_.Contains('Version')}

这会有效,但正如我所提到的,var看起来像这样

<Version>Intel(R) Xeon(R) CPU E5-2609 v3 @ 1.90GHz</Version>

1 个答案:

答案 0 :(得分:0)

(gc discovery.xml | Where-Object {$_.Contains('Version')} | % { $_
-split { $_ -eq '<' -or $_ -eq '>'  } })[2]

替代:

Get-Content | Select-String -Pattern "<Version>(.*)</Version>" | select -ExpandProperty Matches | Select
-ExpandProperty Groups | Select -Expand Value -Last 1

或者:

(gc dicovery.xml -Raw | Select-Xml -XPath "/Processors/Processor/Version/text()[1]").Node.Value