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

Comment out appSettings on web.config using PowerShell

我想知道是否有人可以 使用 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 书呆子) 可以帮助我。非常感谢!

尝试这样的事情:

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

在 V3 及更高版本中很容易做到。

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

希望对您有所帮助!