简体   繁体   中英

Adding a Line In An XML Code Using PowerShell

I need to add a tag in my XML code using PowerShell.

The XML code is an ASP.NET config file

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <runtime>
    <legacyUnhandledExceptionPolicy enabled="false" />
    <legacyImpersonationPolicy enabled="true" />
    <alwaysFlowImpersonationPolicy enabled="false" />
    <SymbolReadingPolicy enabled="1" />
    <shadowCopyVerifyByTimestamp enabled="true" />
  </runtime>
  <startup useLegacyV2RuntimeActivationPolicy="true" />
</configuration>

I Need to add <generatePublisherEvidence_enabled = "False" /> after <runtime> and do it using PowerShell.

$aspnetConfig= 'C:\Users\Desktop\Aspnet.config' 
$doc = (Get-Content $aspnetConfig) -as [Xml] $newXMlElement = $doc.CreateAttribute("getpublisherEvidence_enabled") 
$newXmlElement.Value = "False" 
$doc.configuration.runtime.Attributes.Append($newXMlElement)  
$doc.save($aspnetConfig)

I'm assuming that by <generatePublisherEvidence_enabled = "False" /> you actually mean that underscore is a space so that it becomes <generatePublisherEvidence enabled = "False" /> . If that's the case, this is how you would do that.

$aspnetConfig= "C:\Users\Desktop\Aspnet.config"
$doc = (Get-Content $aspnetConfig) -As [Xml]
$newXmlElement = $doc.CreateElement('generatePublisherEvidence')
$newXMlElement.SetAttribute('enabled','False')
$doc.configuration.runtime.InsertBefore($newXMlElement, $doc.configuration.runtime.legacyUnhandledExceptionPolicy)
$doc.Save($aspnetConfig)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM