代码之家  ›  专栏  ›  技术社区  ›  SeveredTRUTH

无法使用PowerShell从XML文件中的“Set”元素检索信息

  •  0
  • SeveredTRUTH  · 技术社区  · 7 月前

    我正试图从'Set'元素内的name=“KeyStorePath”属性中检索值。见下文: 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>

    以下是我的PowerShell脚本中的代码片段,我试图从中提取所需的信息:

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

    如果我尝试查看$jettyPath的值,它会显示为空白/空。如果我试着看看$jetty的价值。配置。设置后,我看到了以下信息:

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

    XML文件的结构是否有问题,还是我处理得不对?

    另外,我可以使用$jetty毫无问题地检索“Call”元素。配置。呼叫

    2 回复  |  直到 7 月前
        1
  •  2
  •   Mathias R. Jessen    7 月前

    要定位 <Set /> 具有该属性的节点 name 设置为值 KeyStorePath ,使用以下XPath表达式:

    $keyStorePathSet = $jetty.SelectSingleNode("//Set[@name = 'KeyStorePath']")
    $keyStorePathSet.InnerText   # "D:\VALUE\ADM\VALUE.keystore"
    
        2
  •  1
  •   js2010    7 月前

    看起来“set”也是XmlElement或Array对象中的一个方法。不带括号的方法名将显示定义。即使引用set也无济于事。我不确定另一种解决方法。另外,在引号中加上#text,这样就不会被视为评论。您可以将foreach对象缩写为%,where对象缩写为?。

    [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