2

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

enter image description here

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Iggy Castillo
  • 75
  • 1
  • 9
  • May be worth your attention that there's 32 and 64 bit versions of the machine config in ...\Framework\ and ...\Framework64\ folders. I found running my change script similar to following answer very useful https://stackoverflow.com/a/44853234. – LordMsz Jun 30 '23 at 10:39

1 Answers1

5

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()
user4003407
  • 21,204
  • 4
  • 50
  • 60
  • Thank you so much for getting back to me PetSerAI, sorry I didn't get back to you sooner. I just tried your solution and it partially works. it places the System.Web in its correct place and the spacing and all is perfect, but it does not add the system.net section at all and does not multiply the values. I cant seem to figure out how to add the screenshot for the error, but it is basically the same error, does not contain a method named 'op_Multiply' – – Iggy Castillo Oct 10 '16 at 13:33
  • Are you have only one processor? What is `$numberOfCores` content for you? – user4003407 Oct 10 '16 at 16:10
  • Hello PetSerAI, I need for it to pull the number of cores because it will not always be the same machine so the cores may differ and with that the optimization would differ. – Iggy Castillo Oct 11 '16 at 18:35
  • @IggyCastillo What is concrete content of `$numberOfCores` on any PC where script failing with error? – user4003407 Oct 11 '16 at 18:44
  • I see why you are asking that question, that part I just got corrected yesterday, by changing that line to – Iggy Castillo Oct 11 '16 at 18:50
  • I see why you are asking that question, that part i just got corrected yesterday, by change that line to $numberOfCores = Get-WmiObject -class win32_processor | Measure-Object numberOfCores -Sum | Select-Object -ExpandProperty sum – Iggy Castillo Oct 11 '16 at 18:51
  • and there is never a concrete amount of processors, unless of course you count that there must always be one. One thing I am stuck at now is since I create this [xml]$systemnetxml = @" "@ If an error or something occurs, that section will always be there, so if the script is run again, is there a way I can remove that section, like I did here $node.RemoveChild(($node.SelectSingleNode("httpRuntime"))) | Out-Null – Iggy Castillo Oct 11 '16 at 18:54
  • `$connectionManagement.SectionInformation.RevertToParent()` – user4003407 Oct 11 '16 at 19:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125447/discussion-between-iggy-castillo-and-petseral). – Iggy Castillo Oct 11 '16 at 19:50
  • Thank you for your help PetSerAI but I was able to get it with the following lines – Iggy Castillo Oct 13 '16 at 21:20