在 powershell 中写入 machine.config 文件 [英] Writing to the machine.config file in powershell

查看:98
本文介绍了在 powershell 中写入 machine.config 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个脚本上工作了很长一段时间,但不得不把它搁置一旁,因为我不能花时间在它上面.我现在可以更专注于它,它似乎不太正确,希望我能得到一些帮助.

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.

此脚本旨在首先删除 machine.config 中的一个部分,即 ,ProcessModel autoConfig="true"/> 行

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

然后将以下内容写回 processModel

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"/>

那么这里就有点棘手了,我希望它将 90 和 80 乘以虚拟机上的 CPU 内核数.例如,如果机器有 4 个内核,它将读取

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>

和上一个一样,我需要将 200 乘以虚拟机的 CPU 内核数.例如,如果机器有 4 个内核,它将读取

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>

我的代码所做的是将 processModel 部分写出来,但它对我来说并没有增加,而且它似乎没有添加 maxconnection 数,只是留下了一个空格.这是我到目前为止的代码

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.

出现以下错误

此外,它扭曲了 machine.config 文件的原始格式.Machine.config 位于 Windows Server 2012 R2 文件系统中,因此如果您想在自己的机器上测试该文件,请确保先对该文件进行备份.我不希望你把自己的机器搞砸,试图帮助我.

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.

推荐答案

为什么不使用设计用于配置的类?

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()

这篇关于在 powershell 中写入 machine.config 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆