简体   繁体   English

使用 PowerShell 按顺序获取 XML 节点和子节点

[英]Get XML Nodes and SubNodes by an order Using PowerShell

I am facing some challenges to get the nodes and sub-nodes by an order.我面临着按订单获取节点和子节点的一些挑战。 Could you please answer?请问可以回答吗?

MY XML:我的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<sequence version="3.10">
  <referenceList>
    <reference package="PSOO01434" />
  </referenceList>
  <group name="STEP-1 - Set TS variables" description="">
    <step type="SMS_TaskSequence_RunCommandLineAction" name="STEP-2 - Build TS variables" description="" runIn="WinPEandFullOS" successCodeList="0 3010" retryCount="0" runFromNet="false">
      <action>smsswd.exe /run:PSOO01434 Powershell.exe -executionpolicy Bypass -file .\BuildTSVariable.ps1 --AdvID %_SMSTSAdvertID%</action>
      <defaultVarList>
        <variable name="CommandLine" property="CommandLine" hidden="true">Powershell.exe -executionpolicy Bypass -file .\BuildTSVariable.ps1 --AdvID %_SMSTSAdvertID%</variable>
        <variable name="SMSTSDisableWow64Redirection" property="DisableWow64Redirection">true</variable>
        <variable name="PackageID" property="PackageID" hidden="true">PSOO01434</variable>
        <variable name="_SMSTSRunCommandLineAsUser" property="RunAsUser">false</variable>
        <variable name="SuccessCodes" property="SuccessCodes" hidden="true">0 3010</variable>
        <variable name="SMSTSRunCommandLineUserPassword" property="UserPassword" />
      </defaultVarList>
    </step>
  </group>
  <group name="STEP-3 - Install" description="">
    <condition>
      <expression type="SMS_TaskSequence_VariableConditionExpression">
        <variable name="Operator">equals</variable>
        <variable name="Value">1</variable>
        <variable name="Variable">_Install</variable>
      </expression>
    </condition>
    <step type="SMS_TaskSequence_RunCommandLineAction" name="STEP-4 -Run Command Line" description="" runIn="WinPEandFullOS" successCodeList="0 3010" retryCount="0" runFromNet="false">
      <action>smsswd.exe /run: msg Admin "Install Group executed"</action>
      <defaultVarList>
        <variable name="CommandLine" property="CommandLine" hidden="true">msg Admin "Install Group executed"</variable>
        <variable name="SMSTSDisableWow64Redirection" property="DisableWow64Redirection">false</variable>
        <variable name="_SMSTSRunCommandLineAsUser" property="RunAsUser">false</variable>
        <variable name="SuccessCodes" property="SuccessCodes" hidden="true">0 3010</variable>
      </defaultVarList>
    </step>
  </group>
  <group name="STEP-5 - Uninstall" description="">
    <condition>
      <expression type="SMS_TaskSequence_VariableConditionExpression">
        <variable name="Operator">equals</variable>
        <variable name="Value">1</variable>
        <variable name="Variable">_Uninstall</variable>
      </expression>
    </condition>
    <step type="SMS_TaskSequence_RunCommandLineAction" name="STEP-6 - Run Command Line" description="" runIn="WinPEandFullOS" successCodeList="0 3010" retryCount="0" runFromNet="false">
      <action>smsswd.exe /run: msg Admin "Uninstall Group executed"</action>
      <defaultVarList>
        <variable name="CommandLine" property="CommandLine" hidden="true">msg Admin "Uninstall Group executed"</variable>
        <variable name="SMSTSDisableWow64Redirection" property="DisableWow64Redirection">false</variable>
        <variable name="_SMSTSRunCommandLineAsUser" property="RunAsUser">false</variable>
        <variable name="SuccessCodes" property="SuccessCodes" hidden="true">0 3010</variable>
      </defaultVarList>
    </step>
    <group name="STEP-7 - Group" description="">
      <step type="SMS_TaskSequence_RunCommandLineAction" name="STEP-8 - Run Command Line" description="" runIn="WinPEandFullOS" successCodeList="0 3010" retryCount="0" runFromNet="false">
        <action>smsswd.exe /run: Cmd.exe</action>
        <defaultVarList>
          <variable name="CommandLine" property="CommandLine" hidden="true">Cmd.exe</variable>
          <variable name="SMSTSDisableWow64Redirection" property="DisableWow64Redirection">false</variable>
          <variable name="_SMSTSRunCommandLineAsUser" property="RunAsUser">false</variable>
          <variable name="SuccessCodes" property="SuccessCodes" hidden="true">0 3010</variable>
        </defaultVarList>
      </step>
    </group>
  </group>
</sequence>

Question: How to get Nodes and Sub-nodes by an order as shown below?问题:如何按如下所示的顺序获取节点和子节点?

I am expecting the output from this XML like below:我期待这个 XML 的输出如下:

需要输出

You could do something like the following, which should get you started:您可以执行以下操作,这应该可以帮助您入门:

$x = [xml](Get-Content myxml.xml)
$x.SelectNodes('//group') | Foreach-Object {
    [pscustomobject]@{Order = $_.Name -replace 'STEP-(\d+).*','$1'
                      Name  = $_.Name
                      Node  = 'Group'
    } 
    $_.Step | Foreach-Object {
    [pscustomobject]@{Order = $_.Name -replace 'STEP-(\d+).*','$1'
                      Name  = $_.Name
                      Node  = 'Step'
    }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM