【问题标题】:How can I add a section to a web.config using PowerShell?如何使用 PowerShell 将部分添加到 web.config?
【发布时间】:2012-05-07 17:32:39
【问题描述】:

如果 web.config 中的 system.serviceModel 尚不存在,我该如何添加该部分?

之前:

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

之后:

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

【问题讨论】:

  • 为什么要从 PowerShell 执行此操作?它似乎不是适合这项工作的工具。
  • 因为是自动化部署服务,手够了。

标签: xml sharepoint powershell web-config


【解决方案1】:

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

请注意,此方法仅适用于您要注入的 XML 具有单个根元素 &lt;bindings&gt;

【讨论】:

  • 如何从打开的 XML 文件中使用它? $xml = [xml](Get-Content $MainBinding) # $MainBinding - web.config 文件中的变量 - C:\web.config $xml.Save($MainBinding)
  • 在上面的例子中,$origXml一个打开的 XML 文件。 $newXml 只是一个有效 XML 的字符串片段。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-13
  • 2011-05-14
  • 1970-01-01
  • 1970-01-01
  • 2019-10-07
  • 1970-01-01
相关资源
最近更新 更多