Search code examples
xmlpowershellpowershell-5.1

Unable to Retrieve Information from a 'Set' Element within an XML File Using PowerShell


I am trying to retrieve the value from the name="KeyStorePath" attribute, which is within a 'Set' element. See below: KeyStorePath

<?xml version="1.0" encoding="UTF-8"?>
<!-- ============================================================= --><!-- SSL ContextFactory configuration                              --><!-- ============================================================= --><!--
  To configure Includes / Excludes for Cipher Suites or Protocols see tweak-ssl.xml example at
     https://www.eclipse.org/jetty/documentation/current/configuring-ssl.html#configuring-sslcontextfactory-cipherSuites
--><!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.util.ssl.SslContextFactory$Server" id="sslContextFactory">
  <!-- Eliminate Old / Insecure / Anonymous Ciphers -->
  <Call name="addExcludeCipherSuites">
    <Arg>
      <Array type="String">
        <Item>.*NULL.*</Item>
        <Item>.*RC4.*</Item>
        <Item>.*MD5.*</Item>
        <Item>.*DES.*</Item>
        <Item>.*DSS.*</Item>
      </Array>
    </Arg>
  </Call>
  <!-- Eliminate Insecure Protocols -->
  <Call name="addExcludeProtocols">
    <Arg>
      <Array type="java.lang.String">
        <Item>SSL</Item>
        <Item>SSLv2</Item>
        <Item>SSLv2Hello</Item>
        <Item>SSLv3</Item>
      </Array>
    </Arg>
  </Call>
  <Set name="KeyStorePath">D:\VALUE\ADM\VALUE.keystore</Set>
  <Set name="KeyStorePassword">REMOVED</Set>
</Configure>

Here is the snippet in my PowerShell script where I'm trying to pull that the needed information:

[xml]$jetty = gc "$CADPathFull\bin\conf\jetty\jetty-ssl-context.xml"
$jettyPath = $jetty.Configure.Set | Where-Object Name -EQ 'KeyStorePath'

If I try to see the value of $jettyPath, it comes up blank/empty. If I try to see the value of $jetty.Configure.Set, I see this information:

OverloadDefinitions
-------------------
void Set(int , System.Object )

Is there something wrong with the structuring of the XML file or am I approaching this incorrectly?

On a side note, I am able to retrieve the Call elements without an issue using $jetty.Configure.Call


Solution

  • Looks like 'set' is also a method in an XmlElement or Array object. A method name without the parentheses shows you the definitions. Even quoting set didn't help. I'm not sure of another workaround. Also put #text in quotes so it's not taken as a comment. You can abbreviate foreach-object as % and where-object as ?.

    [xml]$xml = get-content file.xml
    $xml.configure | foreach-object set | where-object Name -EQ KeyStorePath | 
      foreach-object '#text'
    
    D:\VALUE\ADM\VALUE.keystore
    
    $xml.configure.gettype()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Object[]                                 System.Array
    
    
    $xml.configure | get-member set                                                                  
    
       TypeName: System.Xml.XmlElement
    
    Name MemberType Definition
    ---- ---------- ----------
    Set  Property   System.Object[] Set {get;}
    
    
    $xml.configure[0] -eq '' # ???
    
    True
    
    
    select-xml '/Configure/Set[@name="KeyStorePath"]' file.xml | % node
    
    name         #text
    ----         -----
    KeyStorePath D:\VALUE\ADM\VALUE.keystore