I want to add existing XML nodes from one file into another XML file by using PowerShell. (Edit the Web.config from some webservices). I tried many ways but I don`t get it running, any help would be appreciated!
My File1 contains some code like:
<?xml version="1.0"?>
<services>
<service name="MyService.HelloWorld">
<endpoint binding="wsHttpBinding" bindingConfiguration="NewBinding0" bindingNamespace="http://tempuri.org/HelloWorld/" contract="MyService.HelloWorld" />
</service>
<service name="MyService.HelloMars">
<endpoint binding="wsHttpBinding" bindingConfiguration="NewBinding0" bindingNamespace="http://tempuri.org/HelloMars/" contract="MyService.HelloMars" />
</service>
.........
Now I want to add all the services into another xml file (the Web.config) and there into a specific node.
My File2 contains some code like:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="MyServices.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</sectionGroup>
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
............ (some more code)
<diagnostics>
<messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"
maxMessagesToLog="2000"/>
</diagnostics>
<services>
**(HERE I WANT TO ADD MY SERVICES!)**
</services>
I also need to modify the imported services, by adding them an extra parameter (Endpoint), so that they look like this: (adding the address parameter)
<service name="MyServices.HelloWorld">
<endpoint binding="wsHttpBinding" bindingConfiguration="NewWsHttpBinding" bindingNamespace="http://tempuri.org/HelloWorld/"
address="https://...../HelloWorld.svc" contract="MyServices.HelloWorld"/>
</service>
I tried many ways I found on the internet, but none worked. The last thing I tried to at least merge the files was:
$webConfig = [xml](Get-Content C:\...\Web.config)
$webConfigNode = $webConfig.configuration.system.serviceModel.services
$servicesToAdd = [xml](Get-Content C:\...\services.config)
$servicesToAddNode = $servicesToAdd.services
while ($servicesToAddNode.HasChildNodes)
{
$cn = $servicesToAddNode.FirstChild
$cn = $servicesToAddNode.RemoveChild($cn)
$cn = $webConfigNode.OwnerDocument.importnode($cn)
$webConfigNode.AppendChild($cn)
}
but I get the following error message:
It is not possible to run a method for an expression which is null
+ $cn = $webConfig.OwnerDocument.importnode($cn)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Thanks for any help!