首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PowerShell按顺序获取XML节点和SubNodes

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

Stack Overflow用户
提问于 2020-04-02 22:50:46
回答 1查看 149关注 0票数 0

我面临着按顺序获取节点和子节点的一些挑战。你能回答一下吗?

我的XML:

代码语言:javascript
复制
<?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>

问:如何按顺序获取节点和子节点,如下所示?

我期望此XML的输出如下所示:

EN

回答 1

Stack Overflow用户

发布于 2020-04-03 01:13:30

您可以执行类似于以下操作的操作,这将使您开始:

代码语言:javascript
复制
$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'
    }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60994432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档