使用PowerShell注释掉web.config上的appSettings

时间:2017-06-23 07:37:46

标签: powershell web-config

我想知道是否有人可以使用PowerShell 对appSettings进行评论。下面的示例代码:(只是一个示例,而不是我的代码)

<configuration>
   <connectionStrings>
      <add name="TestDBEntities" connectionString="metadata=res://*/TestProject.csdl|res://*/TestProject.ssdl|res://*/TestProject.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SQL01;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
   </connectionStrings>
   <appSettings>
      <add key="SCVMMServerName" value="VMM01" />
      <add key="SCVMMServerPort" value="8100" />
   </appSettings>
</configuration>

我想对此部分发表评论:

<add key="SCVMMServerName" value="VMM01" />

结果如下:

<configuration>
   <connectionStrings>
      <add name="TestDBEntities" connectionString="metadata=res://*/TestProject.csdl|res://*/TestProject.ssdl|res://*/TestProject.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SQL01;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
   </connectionStrings>
   <appSettings>
      <!--<add key="SCVMMServerName" value="VMM01" />-->
      <add key="SCVMMServerPort" value="8100" />
   </appSettings>
</configuration>

希望你们(尤其是PowerShell的书呆子)可以帮助我。非常感谢!

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

$XmlDocument = [xml](Get-Content -Path "U:\test.xml")
$node = $XmlDocument.SelectNodes('//SomeNode') | Where-Object{$_.Key -eq "test2"}
$node.ParentNode.InnerXml = $node.ParentNode.InnerXml.Replace($node.OuterXml, $node.OuterXml.Insert(0, "<!--").Insert($node.OuterXml.Length+4, "-->"))
$XmlDocument.Save("U:\test.xml")

答案 1 :(得分:1)

V3及以上版本相当容易。

(Get-Content c:\test.txt).replace('[Add value here]', 'New value') | Set-Content c:\test.txt

希望有所帮助!