views:

96

answers:

2

I'm trying to write a powershell script that updats each of the DiagnosticsConnectionString and DataConnectionString values below, but I can't seem to find each individual Role node using

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

doing echo $serviceconfig.ServiceConfiguration.Role lists out both Role nodes for me so I know it is working up to that point, but after that I am not having much success.

where $serviceConfig contains the below XML:

<?xml version="1.0"?>
<ServiceConfiguration serviceName="MyService"  xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration"&gt;
  <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>
+1  A: 

I would skip the XPath and just filter with PowerShell.

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

At this point if we do something like this...

$serviceConfig.ServiceConfiguration.Role[1].ConfigurationSettings.OuterXml

We'll get this...

<ConfigurationSettings xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration"&gt;
  <Setting name="DiagnosticsConnectionString" value="sup" />
  <Setting name="DataConnectionString" value="really long string 2" />
</ConfigurationSettings>
dahlbyk
How do I get down to the Setting after that?
David Osborn
A: 

Did you ever find an answer to this using XPath ? I am having the same problem, and I am sure it has something to do with the namespace, but after fiddling for 2 hours, i can't event select the ServiceConfiguration (root) node with SelectSingleNode.

Mark Richards
You really should have put that as a comment, but anyways I ended up just using the Powershell filter.
David Osborn