Igg*_*llo 5 xml powershell machine.config powershell-3.0 powershell-4.0
Powershell和Machine.config帮助
我对powershell很新,如果可能的话我需要一个快速的手(我相信这是一个常见的句子).我正在编写一个优化服务器成为Web服务器的脚本,我需要使用powershell写入machine.configs.我也需要所有优化,我不需要任何帮助.
我一直想弄清楚一个多月,还有很多谷歌搜索,我真的找不到解决方案,所以我想找专家.希望我也能在PowerShell中获得好处并在某些方面做出贡献.
到目前为止,我已经取得了令人难以置信的进展,并且已经完成了所有的优化和大部分的PowerShell,但我仍然坚持脚本中的一部分
我需要得到机器有多少cpu核心,我有这条线
$ property ="numberOfCores"Get-WmiObject -class win32_processor -Property $ property | Select-Object -Propert $属性
这告诉我我有多少核心,这正是我需要的东西但是一旦我拥有机器有多少核心,我需要写入machine.config一些值.
在system.web下,它具有这些值
<system.web>
<processModel autoConfig="true"/>
Run Code Online (Sandbox Code Playgroud)
我需要用下面列出的值覆盖已存在的值
<system.web>
<processModel maxWorkerThreads="370" maxIoThreads="370" minWorkerThreads="50" minIoThreads="50"/>
<httpRuntime minFreeThreads="90" minLocalRequestFreeThreads="80"/>
Run Code Online (Sandbox Code Playgroud)
除了写那条线(我无法弄清楚怎么做)之外,我需要将minfreethreads乘以CPU核心数并将该值写入90的位置,并将minLocalRequestFreeThreads 80写入相同的值
因此,例如,如果计算看到2个核,则它将写入以下行
<processModel maxWorkerThreads="370" maxIoThreads="370" minWorkerThreads="50" minIoThreads="50"/>
<httpRuntime minFreeThreads="180" minLocalRequestFreeThreads="160"/>
Run Code Online (Sandbox Code Playgroud)
在那之后,我需要补充一下
<system.net>
<connectionManagement>
<add address = "*" maxconnection = "200" />
</connectionManagement>
</system.net>
Run Code Online (Sandbox Code Playgroud)
和以前一样,然后用cpu核心和200的乘法值替换200.我希望不要问太多,我不知道如何写入xml文件,然后再乘以核心并获取该值并在那里添加?
所以它会喜欢这个
<system.net>
<connectionManagement>
<add address = "*" maxconnection = "400" />
</connectionManagement>
</system.net>
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我一把吗?
编辑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>
'@
Run Code Online (Sandbox Code Playgroud)
编辑1/11
实际上它失败了,带有消息
方法调用失败,因为[System.Object []]不包含名为"op_Multiply"的方法.在C:\ Install\Pre4.ps1:124 char:1 + $ httpRuntimexml.setAttribute("minFreeThreads",90*$ numberOfCores)+ ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation :(op_Multiply:字符串)[],的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")
Run Code Online (Sandbox Code Playgroud)
没怎么使用过 XML 和 PowerShell,但这似乎可以满足您的需求。将文件加载为 XML。删除有问题的两个元素,以便我们可以根据需要构建它们。最后,我们在配置元素下添加元素和相应的详细信息。
如果值在提交之前相乘,您将看到几个涵盖该值的乘法实例。在您的示例中,您对字符串进行了执行乘法,该字符串只是简单地复制了它。考虑以下两个示例。
PS C:\Users\mcameron> "200" * 2
200200
PS C:\Users\mcameron> 200 * 2
400
Run Code Online (Sandbox Code Playgroud)
根据您认为合适的方式更改您的路径。你会在最后看到我写到一个临时位置。我敦促您在测试时也这样做。
$numberOfCores = Get-WmiObject -class win32_processor numberOfCores | Select-Object -ExpandProperty numberOfCores
$path = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machineTest.config"
[xml]$machineConfig = Get-Content $path
# Remove the elements we are going to be replacing
$node = $machineConfig.SelectNodes("/configuration/system.web")
$node.RemoveChild(($node.SelectSingleNode("processModel"))) | Out-Null
# Create the element processModel and set attributes
$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
# Create the element httpRuntime and set attributes. Adjust values based on number of cores
$httpRuntimexml = $machineConfig.CreateElement("httpRuntime")
$httpRuntimexml.setAttribute("minFreeThreads",(90 * $numberOfCores))
$httpRuntimexml.setAttribute("minLocalRequestFreeThreads",(80 * $numberOfCores))
$node.AppendChild($httpRuntimexml) | Out-Null
# Build the <system.net> section
[xml]$systemnetxml = @"
<system.net>
<connectionManagement>
<add address = "*" maxconnection = "$(200 * $numberOfCores)" />
</connectionManagement>
</system.net>
"@
# Import into config
$machineConfig.configuration.AppendChild($machineConfig.ImportNode($systemnetxml."system.net",$true)) | Out-Null
# Save changes
$machineConfig.Save("c:\temp\testing.xml")
# Change back to $path to write back to original file.
Run Code Online (Sandbox Code Playgroud)
您还将看到Out-Null哪些内容可以抑制正在创建的元素的输出。它不会改变文件发生的情况。