我正在尝试编写一个Powershell脚本来更新下面的每个DiagnosticsConnectionString和DataConnectionString值,但是我似乎找不到使用

$serviceconfig.ServiceConfiguration.SelectSingleNode("Role[@name='MyService_WorkerRole']")

进行echo $serviceconfig.ServiceConfiguration.Role会为我列出两个Role节点,所以我知道它可以正常工作,但是之后我没有太大的成功。

其中$ serviceConfig包含以下XML:
<?xml version="1.0"?>
<ServiceConfiguration serviceName="MyService"  xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
  <Role name="MyService_WorkerRole">
    <Instances count="1" />
    <ConfigurationSettings>
      <Setting name="DiagnosticsConnectionString" value="really long string" />
      <Setting name="DataConnectionString" value="really long string 2" />
    </ConfigurationSettings>
  </Role>
  <Role name="MyService_WebRole">
    <Instances count="1" />
    <ConfigurationSettings>
      <Setting name="DiagnosticsConnectionString" value="really long string 3" />
      <Setting name="DataConnectionString" value="really long string 4" />
    </ConfigurationSettings>
  </Role>
</ServiceConfiguration>

最佳答案

我将跳过XPath,仅使用PowerShell进行过滤。

$serviceConfig.ServiceConfiguration.Role |
    ? { $_.name -eq 'MyService_WorkerRole' } |
    % { $_.ConfigurationSettings.Setting } |
    ? { $_.name -like 'Diag*' } |
    % { $_.value = 'sup' }

在这一点上,如果我们做这样的事情...
$serviceConfig.ServiceConfiguration.Role[1].ConfigurationSettings.OuterXml

我们会得到这个...
<ConfigurationSettings xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
  <Setting name="DiagnosticsConnectionString" value="sup" />
  <Setting name="DataConnectionString" value="really long string 2" />
</ConfigurationSettings>

10-08 14:06