You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell无法从XML文件的Set元素中获取信息

获取Jetty配置XML中Set元素值的问题及解决方法

问题描述

要从Jetty的SSL配置XML中提取<Set name="KeyStorePath">元素的内容,XML结构如下:

<?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代码尝试提取时,$jettyPath返回空值:

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

查看$jetty.Configure.Set时,输出的是方法重载定义而非XML元素:

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

但$jetty.Configure.Call能正常获取到<Call>元素的内容。

问题原因

这是因为Set是PowerShell的内置方法名,当XML节点名称与PowerShell对象的方法名冲突时,PowerShell会优先将$jetty.Configure.Set解析为Configure对象的Set方法,而不是XML中的<Set>节点集合,导致无法直接获取目标元素。

解决方法

有两种可行的方式绕过这个命名冲突:

方法1:使用GetElementsByTagName方法

直接通过标签名获取所有<Set>元素,再筛选目标属性:

[xml]$jetty = Get-Content "$CADPathFull\bin\conf\jetty\jetty-ssl-context.xml"
$targetSet = $jetty.Configure.GetElementsByTagName('Set') | Where-Object { $_.name -eq 'KeyStorePath' }
# 获取对应的值
$keyStorePath = $targetSet.InnerText

方法2:用引号包裹节点名

在PowerShell中,用单引号包裹与关键字冲突的节点名,强制解析为XML节点:

[xml]$jetty = Get-Content "$CADPathFull\bin\conf\jetty\jetty-ssl-context.xml"
$targetSet = $jetty.Configure.'Set' | Where-Object { $_.name -eq 'KeyStorePath' }
$keyStorePath = $targetSet.InnerText

两种方法都能正确定位到目标<Set>元素,通过.InnerText即可提取到KeyStorePath对应的路径值。

内容的提问来源于stack exchange,提问作者SeveredTRUTH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 09:25:17