Powershell 和 Machine.config 帮助
我对powershell很陌生,如果可能的话,我需要一个快速的手(我相信这是一个常见的句子)。我正在编写一个优化服务器以成为网络服务器的脚本,我需要使用 powershell 写入 machine.configs。我也有所有需要的优化,我不需要帮助。
我一直在努力解决一个多月,还有很多谷歌搜索,我真的找不到解决方案,所以我想来找专家。希望我也能在 powershell 中变得更好,并在某些时候做出贡献。
到目前为止,我已经取得了令人难以置信的进展,并且已经完成了所有的优化和大部分的 powershell,但我被困在脚本的一部分
我需要得到机器有多少个cpu核心,我有这条线
$property = "numberOfCores" Get-WmiObject -class win32_processor -Property $property | 选择对象-属性 $property
这告诉我我有多少个核心,这正是我需要的,但是一旦我有机器有多少个核心,我需要向 machine.config 写入一些值。
在 system.web 下,它有这些值
<system.web>
<processModel autoConfig="true"/>
我需要用下面列出的这个值覆盖已经存在的值
<system.web>
<processModel maxWorkerThreads="370" maxIoThreads="370" minWorkerThreads="50" minIoThreads="50"/>
<httpRuntime minFreeThreads="90" minLocalRequestFreeThreads="80"/>
除了在那里写那行(我不知道该怎么做)之外,我需要将 minfreethreads 乘以 CPU 内核的数量,并将该值写在 90 的位置,对于 minLocalRequestFreeThreads 80 也是如此
因此,例如,如果计算看到 2 个核心,它将编写以下行
<processModel maxWorkerThreads="370" maxIoThreads="370" minWorkerThreads="50" minIoThreads="50"/>
<httpRuntime minFreeThreads="180" minLocalRequestFreeThreads="160"/>
之后,我需要添加
<system.net>
<connectionManagement>
<add address = "*" maxconnection = "200" />
</connectionManagement>
</system.net>
和之前一样,然后将200替换为cpu cores和200的乘积值。希望不要问太多,我不知道如何写入xml文件,然后也乘以cores并取那个值并在那里添加?
所以它会喜欢这个
<system.net>
<connectionManagement>
<add address = "*" maxconnection = "400" />
</connectionManagement>
</system.net>
谁能帮我一把?
编辑 1/4
这是我到目前为止的代码,我很远,我正在逐行处理它,所以有些东西可能行不通,但我认为我走在正确的道路上
$xml = New-Object XML
$xml.Load("C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config")
$Path = "C:\Windows\Microsoft.Net\Framework\V2.0.50727\config"
$File = "machine.config"
$current_path = $path + "\" + $file
$text = (get-content ($current_path))
$xml = [XML] (get-content ($current_path))
$p.RemoveAttribute("autoConfig")
$p = $xml.configuration."system.web".processModel
$p.SetAttribute("maxWorkerThreads", "370")
$p.SetAttribute("maxIoThreads", "370")
$p.SetAttribute("minWorkerThreads", "50")
$p = $xml.configuration."system.web".httpRunTime
$p.SetAttribute("minFreeThreads", "90")
$p.SetAttribute("minLocalRequestFreeThreads", "80")
$processor = (Get-CimInstance Win32_processor -Property NumberOfLogicalProcessors | Select -ExpandProperty "NumberOfLogicalProcessors")
$minFT = $processor * 90
$minFT = [string]$minFT
$minFT * 2
$p.SetAttribute("minFreeThreads", [string]$minFT)
$xml_content = [xml]@'
<system.net>
<connectionManagement>
<add address = "*" maxconnection = "200" />
</connectionManagement>
</system.net>
'@
编辑 1/11
实际上它失败了,带有消息
方法调用失败,因为 [System.Object[]] 不包含名为“op_Multiply”的方法。在 C:\Install\Pre4.ps1:124 char:1 + $httpRuntimexml.setAttribute("minFreeThreads",90 * $numberOfCores) + ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation : (op_Multiply:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
方法调用失败,因为 [System.Object[]] 不包含名为“op_Multiply”的方法。在 C:\Install\Pre4.ps1:125 char:1 + $httpRuntimexml.setAttribute("minLocalRequestFreeThreads",80 * $numberOfCores) + ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ + CategoryInfo : InvalidOperation: (op_Multiply:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
方法调用失败,因为 [System.Object[]] 不包含名为“op_Multiply”的方法。在 C:\Install\Pre4.ps1:130 char:45 + + ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (op_Multiply:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
- - - 脚本 - - -
$numberOfCores = Get-WmiObject -class win32_processor numberOfCores | Select-Object -ExpandProperty numberOfCores
$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("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
[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")