簡體   English   中英

如何使用PowerShell刪除XML的一部分?

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

下面的腳本正在執行的操作是捕獲計算機上CPU內核的數量。 如果腳本之前已運行過,它將刪除失敗嘗試中可能遺留下來的腳本部分,然后將其寫回。

它會編寫httpruntime,Processmodel以及maxworker線程,maxworkerthreads,maxiothreads,minworkerthreads,miniotheads,然后是minfreethreads,minlocalrequestfreethreeads,然后將minfreetheads和本地requestfreethreads中的90和80乘以CPU內核數。

然后,我希望它刪除存在的發呆狀態,然后編寫結構,添加連接管理,並添加地址和maxconnection字符串,從而將數字加200,然后乘以200倍於CPU核心數。

這是下面的代碼

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

這是此操作涉及的machine.config的更新部分,請注意多余的system.nets

機器配置

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

更改回僅您的代碼,仍然重現相同的部分,但沒有像我前面提到的那樣放入“ connectionManagement:connectionManagement”

規范的方法是從其父節點中刪除該節點:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM