我正在尝试编写一个powershell脚本来更新下面的每个DiagnosticsConnectionString和DataConnectionString值,但我似乎无法使用
找到每个单独的Role节点$serviceconfig.ServiceConfiguration.SelectSingleNode("Role[@name='MyService_WorkerRole']")
执行echo $serviceconfig.ServiceConfiguration.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>
答案 0 :(得分:2)
我会跳过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>