简体   繁体   中英

How can I remove a section of an XML using PowerShell?

What the script below is doing is capture the number of CPU cores on a machine. In case the script had been run before, it deletes portions of the script that may have been left over from failed attempts, then it writes them back.

It writes the httpruntime, Processmodel along with with the maxworker threads, maxworkerthreads, maxiothreads, minworkerthreads, miniotheads, then the minfreethreads, minlocalrequestfreethreeads and then it multiples the 90 and 80 from the minfreetheads and local requestfreethreads by the number of CPU cores.

Then I want it to delete trances of the if it exists then it writes the structure, adding connectionmanagement, and add address along with the maxconnection string, that adds the number 200 and then multiplies the 200 times the number of CPU Cores.

Here is the code below

$numberOfCores = Get-WmiObject -class win32_processor | Measure-Object numberOfCores -Sum | Select-Object -ExpandProperty sum
$path = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config"
[xml]$machineConfig = Get-Content $path
$node = $machineConfig.SelectNodes("/configuration/system.web") 
$node.RemoveChild(($node.SelectSingleNode("httpRuntime"))) | Out-Null
$node.RemoveChild(($node.SelectSingleNode("processModel"))) | Out-Null
$processModelxml = $machineConfig.CreateElement("processModel")
$processModelxml.setAttribute("maxWorkerThreads",370)
$processModelxml.setAttribute("maxWorkerThreads",370)
$processModelxml.setAttribute("maxIoThreads",370)
$processModelxml.setAttribute("minWorkerThreads",50)
$processModelxml.setAttribute("minIoThreads",50)
$node.AppendChild($processModelxml) | Out-Null
$httpRuntimexml = $machineConfig.CreateElement("httpRuntime")
$httpRuntimexml.setAttribute("minFreeThreads",90 * $numberOfCores)
$httpRuntimexml.setAttribute("minLocalRequestFreeThreads",80 * $numberOfCores)
$node.AppendChild($httpRuntimexml) | Out-Null
$node = $systemnetxml.SelectSingleNode('//system.net')
$node.ParentNode.RemoveChild($node)
[xml]$systemnetxml = @"
  <system.net>
    <connectionManagement>
      <add address = "*" maxconnection = "$(200 * $numberOfCores)" />
    </connectionManagement>
  </system.net>
"@
$machineConfig.configuration.AppendChild($machineConfig.ImportNode($systemnetxml."system.net",$true)) | Out-Null
$machineConfig.Save("C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config")

Here is the update portion of the machine.config that this touches, and notice the extra system.nets

machine.config

    <configuration>
      <system.web>
    <processModel maxWorkerThreads="370" maxIoThreads="370" minWorkerThreads="50" minIoThreads="50" />
    <httpRuntime minFreeThreads="180" minLocalRequestFreeThreads="160" />
  </system.web>
  <system.net>
    <connectionManagement>
      <add address="*" maxconnection="400" />
    </connectionManagement>
  </system.net>
  <system.net>
    <connectionManagement>
      <add address="*" maxconnection="400" />
    </connectionManagement>
  </system.net>
</configuration>

changing back to only your code, still reproduces the same section, but not it out puts "connectionManagement : connectionManagement" as i mentioned previously

The canonical way is to remove the node from its parent:

[xml]$machineConfig = Get-Content $path
...
$node = $machineConfig.SelectSingleNode('//system.net')
$node.ParentNode.RemoveChild($node)

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