简体   繁体   中英

Extracting XML nodes and values based on a pattern in PowerShell

New to PowerShell. Have to re-create 100s of proxy rules into a different proxy product. The only option I have to export the rules from the existing proxy is XML.

The XML export is structured as so (2 sections I'm interested in and I put "**" around the nodes I'm interested in):

<libraryContent>
  <lists>
    <entry>
      **<string>com.scur.type.string.2988</string>**
      <list>
        <description></description>
        <content>
          <listEntry>
            **<entry>symphony-docker-external.jfrog.io</entry>**
            **<description>IM4452077</description>**
          </listEntry>
        </content>
      </list>
    </entry>
  </lists>

(100s of those lists)

 <ruleGroup id="291" defaultRights="2" name="Authentication" enabled="true" cycleRequest="true" cycleResponse="false" cycleEmbeddedObject="false" cloudSynced="false">
    <acElements/>
    <description></description>
    <rules>
     **<rule id="3002" enabled="true" name="Allow access to AssetBank">**
        **<description>Catalog Task #SCTASK0020659</description>**
        <actionContainer actionId="com.scur.mainaction.stoprulegroup"/>
        <condition always="false">
          <expressions>
            <conditionExpression openingBracketCount="0" closingBracketCount="0" operatorId="com.scur.operator.isinlist">
              <propertyInstance useMostRecentConfiguration="false" propertyId="com.scur.engine.system.client.ip"/>
              <parameter valueTyp="1" typeId="com.scur.type.list" listTypeId="com.scur.type.ip">
                <value>
                  **<listValue id="com.scur.type.ip.2941"/>**
                </value>
              </parameter>
            </conditionExpression>
            <conditionExpression prefix="AND" openingBracketCount="0" closingBracketCount="0" operatorId="com.scur.operator.equals">
              <propertyInstance useMostRecentConfiguration="false" propertyId="com.scur.engine.system.url.smartmatch">
                <parameters>
                  <entry>
                    <string>com.scur.engine.system.string.domainmagicmatch.domainlist</string>
                    <parameter valueTyp="1">
                      <value>
                        **<listValue id="com.scur.type.string.2988"/>**
                      </value>
                    </parameter>
                  </entry>
                </parameters>
              </propertyInstance>
              <parameter valueTyp="3" valueId="com.scur.type.boolean.true" typeId="com.scur.type.boolean"/>
            </conditionExpression>
          </expressions>
        </condition>
      </rule>
    </rules>
  </ruleGroup>
</libraryContent>

100s of those rules.

I'm basically trying to put together the following information (with examples from the above XML)

  • RuleID: 3002
  • Enabled: true
  • RuleName: Allow access to AssetBank
  • RuleDescription: Catalog Task #SCTASK0020659
  • ListsValueIDs: com.scur.type.ip.2941 , com.scur.type.string.2988

Then for each of the "ListsValuesIDs", gather the childnodes information:

  • ListValuesID: com.scur.type.ip.2988
  • ListEntry: symphony-docker-external.jfrog.io
  • ListDescription: IM4452077

I first tried the following (load the XML and then assign the values I wanted to objects in an array):

[XML]$XMLRuleSet = get-content ruleset.xml
$XMLRuleSet.libraryContent.ruleGroup.rules.rule.id
$array=@();
ForEach ($LibraryContent in $XMLRuleSet) {
    $LibraryContent
    $tempobj = New-Object PSObject -Property @{
        'id' = $LibraryContent.libraryContent.ruleGroup.rules.rule.id;
        'name' = $LibraryContent.libraryContent.ruleGroup.rules.rule.name;
        'enabled' = $LibraryContent.libraryContent.ruleGroup.rules.rule.enabled;
        'ruleDescription' = $LibraryContent.libraryContent.ruleGroup.rules.rule.description;
        'listvalueID' = $LibraryContent.libraryContent.ruleGroup.rules.rule.condition.expressions.conditionExpression.parameter.value.listValue;
  } 

    $array += $tempobj
}

$array

Problem with this is there is always more than 1 ' listValueID ' for each rule so when I try to export the array in CSV farther down my code I get the following:

@{name=System.Object[]; id=System.Object[]; enabled=System.Object[]}

I don't absolutely want to export my values in an array and in CSV (it could be in a file with carriage returns for each values or whatever), this is just what I thought would be best after searching how to work with XML using powershell.

Try getting the first element (if any) with Select -index 0 .

For example: $LibraryContent.libraryContent.ruleGroup.rules.rule.condition.expressions.conditionExpression.parameter.value.listValue | Select -index 0; $LibraryContent.libraryContent.ruleGroup.rules.rule.condition.expressions.conditionExpression.parameter.value.listValue | Select -index 0;

Alternatively you can do ...parameter.value.listValue[0] but unlike Select , this could crash if none is found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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