首页 > 解决方案 > Powershell在一个Select-Object中获取不同级别的节点值

问题描述

有没有办法做一个Select-Object并获取不同级别节点的值?

我需要字段级别的名称和一堆字段,还需要 globalFormat categoryType。有没有办法让它们合二为一Select-Object

XML

<field name="FLD0000001" tableAlias="*SERVER" tableName="*SERVER">
  <order>0</order>
  <version></version>
  <afterEditEvent></afterEditEvent>
  <format></format>
  <globalFormat categoryType="Number" countrySymbolCode="" dateFormat="" timeFormat="" imageFormat="None" decimalPlaces="2" leadingZero="0" isCentury="false" negativeFormat="NegativeSymbol" />
  <columnGroupHeading></columnGroupHeading>
  <dataFieldType>20</dataFieldType>
  <columnHeading><![CDATA[FLD0000001]]></columnHeading>
  <hideHeader>False</hideHeader>
  <groupable>0</groupable>
  <subTotal>0</subTotal>
  <calculation></calculation>
  <jScriptCalculation />
  <editCalculation scriptContent="" scriptDisplayContent="">
    <tokens />
    <triggers />
  </editCalculation>
  <onCalculationEvent></onCalculationEvent>
  <onValidationEvent></onValidationEvent>
  <editableAdvanced></editableAdvanced>
  <addRequired>0</addRequired>
  <splitByRationOn></splitByRationOn>
  <defaultValue></defaultValue>
  <isCalculatable>0</isCalculatable>
  <isUpdatable>0</isUpdatable>
  <visibleAdvanced></visibleAdvanced>
  <editableLevel>0</editableLevel>
  <visibleLevel>10</visibleLevel>
  <fullTypeName></fullTypeName>
  <dataType>NUMERIC</dataType>
  <dataLength>5</dataLength>
  <description source="" format="4"></description>
  <dimensionTitle></dimensionTitle>
  <definition></definition>
  <parameterName></parameterName>
  <cubeDimensionOrder>0</cubeDimensionOrder>
  <subTotalRestrictions></subTotalRestrictions>
  <subTotalExceptions></subTotalExceptions>
  <matrixHeaderValue></matrixHeaderValue>
  <promptRestricted>0</promptRestricted>
  <prompt processId="" />
  <promptOption></promptOption>
  <sortOrderForPrompt>NONE</sortOrderForPrompt>
  <filterForPrompt></filterForPrompt>
  <relatedFields></relatedFields>
  <validateAgainstPrompt>False</validateAgainstPrompt>
  <subGroupId></subGroupId>
  <indexInGroup>0</indexInGroup>
  <widthInFieldArea>2000</widthInFieldArea>
</field>

我基本上想检查dataFieldType,如果是20或21,检查globalFormat categoryType看是否没有,如果是,则在文件中输出为尚未正确设置的Field。但我喜欢那个 xml 中的数百个字段

标签: powershellselect-object

解决方案


您可以与查询Select-Xml结合使用并获取所需的元素和属性值。我相信,您可以指定不同的 s 来访问不同的节点级别,包括元素属性。XPathSelect-ObjectXPath

请参阅Microsoft 文档中的这些示例:

$Path = "$Pshome\Types.ps1xml"
$XPath = "/Types/Type/Members/AliasProperty"
Select-Xml -Path $Path -XPath $Xpath | Select-Object -ExpandProperty Node
Name                 ReferencedMemberName
----                 --------------------
Count                Length
Name                 Key
Name                 ServiceName
RequiredServices     ServicesDependedOn
ProcessName          Name
Handles              Handlecount
VM                   VirtualSize
WS                   WorkingSetSize
Name                 ProcessName
Handles              Handlecount
VM                   VirtualMemorySize
WS                   WorkingSet
PM                   PagedMemorySize
NPM                  NonpagedSystemMemorySize
Name                 __Class
Namespace            ModuleName

例如,如果您想获取globalFormat元素的属性,您可以执行以下操作:

$path = "\test.xml"
$xpath = "/field/globalFormat"
Select-Xml -Path $path -XPath $xpath | Select-Object -ExpandProperty Node

categoryType      : Number
countrySymbolCode :
dateFormat        :
timeFormat        :
imageFormat       : None
decimalPlaces     : 2
leadingZero       : 0
isCentury         : false
negativeFormat    : NegativeSymbol

如果你想同时访问不同的元素和属性,你可以这样做:

$path = "test.xml"
$xpathField = "/field"
$xpathGlobalFormat = "/field/globalFormat"
$field = Select-Xml -Path $path -XPath $xpathField | Select-Object -ExpandProperty Node
$globalFormat = Select-Xml -Path $path -XPath $xpathGlobalFormat | Select-Object -ExpandProperty Node
$field.tableName # this returns *SERVER
$globalFormat.categoryType # this returns Number

推荐阅读