2014-10-27 90 views
0

我想通過使用PowerShell將現有XML節點從一個文件添加到另一個XML文件。 (從一些Web服務編輯Web.config)。 我嘗試了很多方法,但我沒有得到它的運行,任何幫助將不勝感激!將xml節點從一個xml文件添加到另一個具有powershell的xml文件到另一個文件中的特定現有節點

我的文件1包含這樣一些代碼:

<?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> 
 
    .........

現在我想所有的服務添加到另一個XML文件(在Web.config),並有到特定的節點。

我的文件2包含這樣一些代碼:

<?xml version="1.0" encoding="utf-8"?> 
 

 
<configuration> 
 
\t <configSections> 
 
\t \t <sectionGroup name="applicationSettings" 
 
\t \t \t type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
 
\t \t \t <section name="MyServices.Properties.Settings" 
 
\t \t \t \t type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/> 
 
\t \t </sectionGroup> 
 
\t </configSections> 
 
\t <system.web> 
 
\t \t <compilation debug="true" targetFramework="4.0"/> 
 
\t </system.web> 
 
\t <system.serviceModel> 
 

 
    ............ (some more code) 
 
     
 
\t \t <diagnostics> 
 
\t \t \t <messageLogging logEntireMessage="true" logMalformedMessages="true"  logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" 
 
\t \t \t \t maxMessagesToLog="2000"/> 
 
\t \t </diagnostics> 
 
\t \t <services> 
 

 
\t \t **(HERE I WANT TO ADD MY SERVICES!)** 
 
\t \t \t 
 
\t \t </services>  
 

我還需要修改導入的服務,加入他們一個額外的參數(終點),讓它們看起來像這樣:(加入地址參數)

<service name="MyServices.HelloWorld"> 
 
<endpoint binding="wsHttpBinding" bindingConfiguration="NewWsHttpBinding" bindingNamespace="http://tempuri.org/HelloWorld/" 
 
address="https://...../HelloWorld.svc" contract="MyServices.HelloWorld"/> 
 
</service>

我試過很多方法,我發現在互聯網上,但沒有奏效。 我想至少合併文件的最後一句話是:

$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) 
 
}

,但我得到了以下錯誤消息:

It is not possible to run a method for an expression which is null 
 
+  $cn = $webConfig.OwnerDocument.importnode($cn) 
 
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 
    + CategoryInfo   : InvalidOperation: (:) [], RuntimeException 
 
    + FullyQualifiedErrorId : InvokeMethodOnNull

感謝您的幫助!

回答

相關問題