如何使用PowerShell将部分添加到web.config?

时间:2021-08-01 20:19:37

How can I add a section to system.serviceModel in a web.config, if it doesn't already exist?

如何在web.config中添加一个部分到system.serviceModel(如果它尚不存在)?

Before:

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

After:

<system.serviceModel>
 <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
 <bindings>
      <basicHttpBinding>
        <binding name="ValidationServiceSoap" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="Transport">
            <transport clientCredentialType="Ntlm" />
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </basicHttpBinding>
 <bindings>
</system.serviceModel>

1 个解决方案

#1


5  

It's not too bad to do this in PowerShell:

在PowerShell中执行此操作并不算太糟糕:

$origXml = [xml]@'
<configuration>
<system.serviceModel> 
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
</system.serviceModel> 
</configuration>
'@

$newXml = @'
 <bindings> 
      <basicHttpBinding> 
        <binding name="ValidationServiceSoap" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> 
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
          <security mode="Transport"> 
            <transport clientCredentialType="Ntlm" /> 
            <message clientCredentialType="UserName" /> 
          </security> 
        </binding> 
      </basicHttpBinding> 
 </bindings> 
'@

if (!$origXml.configuration.'system.serviceModel'.bindings)
{
    $tempXmlDoc = new-object System.Xml.XmlDocument
    $tempXmlDoc.LoadXml($newXml)
    $newNode = $origXml.ImportNode($tempXmlDoc.DocumentElement, $true)
    $origXml.configuration.'system.serviceModel'.AppendChild($newNode)
}

Note that this approach only works because the XML you want to inject has a single root element <bindings>.

请注意,此方法仅适用,因为要注入的XML具有单个根元素

#1


5  

It's not too bad to do this in PowerShell:

在PowerShell中执行此操作并不算太糟糕:

$origXml = [xml]@'
<configuration>
<system.serviceModel> 
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
</system.serviceModel> 
</configuration>
'@

$newXml = @'
 <bindings> 
      <basicHttpBinding> 
        <binding name="ValidationServiceSoap" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> 
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
          <security mode="Transport"> 
            <transport clientCredentialType="Ntlm" /> 
            <message clientCredentialType="UserName" /> 
          </security> 
        </binding> 
      </basicHttpBinding> 
 </bindings> 
'@

if (!$origXml.configuration.'system.serviceModel'.bindings)
{
    $tempXmlDoc = new-object System.Xml.XmlDocument
    $tempXmlDoc.LoadXml($newXml)
    $newNode = $origXml.ImportNode($tempXmlDoc.DocumentElement, $true)
    $origXml.configuration.'system.serviceModel'.AppendChild($newNode)
}

Note that this approach only works because the XML you want to inject has a single root element <bindings>.

请注意,此方法仅适用,因为要注入的XML具有单个根元素