Writing to the machine.config file in powershell

▼魔方 西西 提交于 2021-02-19 09:11:35

问题


I have been working on this script for quite some time, but had to put it to rest because I could not spend time on it. I can focus more on it now, and it seems to not work quite right, hopefully I can get some help.

This script is intended to first delete a section in the machine.config, the ,ProcessModel autoConfig="true"/> line

$node = $machineConfig.SelectNodes("/configuration/system.web") 
$node.RemoveChild(($node.SelectSingleNode("processModel"))) | Out-Null

Then it write back to the processModel the following

<system.web>
    <processModel maxWorkerThreads="370" maxIoThreads="370" minWorkerThreads="50" minIoThreads="50"/>
<httpRuntime minFreeThreads="90" minLocalRequestFreeThreads="80"/>

Then here it gets a little tricky, I want it to then multiple the 90 and the 80, by the number of CPU Cores on the virtual machine. For example, if the machine had 4 cores it would read

<system.web>
    <processModel maxWorkerThreads="370" maxIoThreads="370" minWorkerThreads="50" minIoThreads="50"/>
<httpRuntime minFreeThreads="360" minLocalRequestFreeThreads="320"/>

After that, towards the bottom of the file, I want it to add the following line

<system.net>
<connectionManagement>
<add address = "*" maxconnection = "200" />
</connectionManagement>
</system.net>

Like on the previous one, I need the 200 to be multiplied by the number of CPU cores of the virtual machine. So for example, if the machine had 4 cores, it would read

<system.net>
<connectionManagement>
<add address = "*" maxconnection = "800" />
</connectionManagement>
</system.net>

What the code i have does is that it write the processModel section out, but it does not multiply for me and it does not seem to add the maxconnection number, just leaves a space. Here is the code i have so far

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

I have been struggling with this for almost a year, I had to get off of it, and coming back to it I find that it does not work.

It has the following error

Also, it distorts the original format of the machine.config file. Machine.config is in Windows Server 2012 R2 file system, so if you wanted to test the file on your own machine, just make sure you make a backup of that file first. I would hate for you to mess up your own machine trying to help me out.


回答1:


Why not use classes, which designed to work with configuration?

$numberOfCores = Get-WmiObject -class win32_processor numberOfCores | Select-Object -ExpandProperty numberOfCores

$machineConfig = [System.Configuration.ConfigurationManager]::OpenMachineConfiguration()

$processModel = $machineConfig.SectionGroups['system.web'].ProcessModel
$processModel.SectionInformation.RevertToParent()
$processModel.MaxWorkerThreads = 370
$processModel.MaxIOThreads = 370
$processModel.MinWorkerThreads = 50
$processModel.MinIOThreads = 50

$httpRuntime = $machineConfig.SectionGroups['system.web'].HttpRuntime
$httpRuntime.MinFreeThreads = 90 * $numberOfCores
$httpRuntime.MinLocalRequestFreeThreads = 80 * $numberOfCores

$connectionManagement = $machineConfig.SectionGroups['system.net'].ConnectionManagement
$connectionManagement.ConnectionManagement.Add((New-Object System.Net.Configuration.ConnectionManagementElement *, (200 * $numberOfCores)))

$machineConfig.Save()


来源:https://stackoverflow.com/questions/39925805/writing-to-the-machine-config-file-in-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!