Powershell help required to write to Machine.config

Powershell and Machine.config Help

I am very new to powershell and I need a quick hand, if at all possible (I'm sure this is a normal sentence). I am writing a script that optimizes the server to become a web server, I need to write to machine.configs using powershell. I also have all the necessary optimizations, I do not need help.

I’ve been trying to find out for more than a month, I also have a lot of Google, I can’t find a solution, so I decided to come to the experts. Hopefully, I can also become better at powershell and contribute at some point.

I have come so far incredibly far and already completed all the optimizations and most of PowerShell, but got stuck on 1 part of the script

  1. I need to find out how many processor cores a machine has, I have this line

    $ property = "numberOfCores" Get-WmiObject -class win32_processor -Property $ property | Property Select-Object -Propert $

This tells me how many cores I have, and this is exactly what I need, but as soon as I have the number of cores per machine, I need to write some values ​​to machine.config.

Under system.web it has these values

<system.web>
    <processModel autoConfig="true"/>

I will need to rewrite an existing value with this listed below

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

Besides writing this line (which I can’t figure out how to do this), I need to multiply minfreethreads by the number of CPU cores and write this value instead of 90 and the same goes for minLocalRequestFreeThreads 80

So, for example, if the calculation sees 2 cores, it will write the following lines

<processModel maxWorkerThreads="370" maxIoThreads="370" minWorkerThreads="50" minIoThreads="50"/>
<httpRuntime minFreeThreads="180" minLocalRequestFreeThreads="160"/>

after that i need to add

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

, 200 200. , , , XML , ?

<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")
+5
1

Havent XML PowerShell, , , , . XML. , , , . , .

, . , . .

PS C:\Users\mcameron> "200" * 2
200200

PS C:\Users\mcameron> 200 * 2
400

. , . .

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

Out-Null, . , .

+2

Source: https://habr.com/ru/post/1622348/


All Articles