Q: I have a windows service that spawns a lot of child processes. There seems to be some limit, around 100, where the process cannot be launched. The CreateProcess() call
We had many Remote Desktop servers we needed to test this on, so we wrote a powershell script to query AD for our RD servers and then apply this registry change. Enjoy
$newValue = "%SystemRoot%\system32\csrss.exe ObjectDirectory=\Windows SharedSection=1024,20480,2048 Windows=On SubSystemType=Windows ServerDll=basesrv,1 ServerDll=winsrv:UserServerDllInitialization,3 ServerDll=sxssrv,4 ProfileControl=Off MaxRequestThreads=16"
$origValue = "%SystemRoot%\system32\csrss.exe ObjectDirectory=\Windows SharedSection=1024,20480,768 Windows=On SubSystemType=Windows ServerDll=basesrv,1 ServerDll=winsrv:UserServerDllInitialization,3 ServerDll=sxssrv,4 ProfileControl=Off MaxRequestThreads=16"
if ($update) {
Clear-Variable update
}
$updateConfirm= [System.Windows.Forms.MessageBox]::Show("Update Desktop Heap Limitation to 2048?" , "Status" , 4)
if ($updateConfirm -eq "YES" ) {
$update = $true
} else {
$revertConfirm= [System.Windows.Forms.MessageBox]::Show("Revert Desktop Heap Limitation to 768?" , "Status" , 4)
if ($revertConfirm -eq "YES" ) {
$update = $false
}
}
if (($updateConfirm -ne "YES") -and ($revertConfirm -ne "YES")) {
Write-Host "User did not specify whether to update or revert Desktop Heap Limitation. Exiting Setup."
Read-Host "Press Enter to exit."
break
}
#Import Active Directory PowerShell module
if (Test-Path C:\Users\${env:USERNAME}\Documents\WindowsPowerShell\Modules\ActiveDirectory\ActiveDirectory.psm1) {
Import-Module ActiveDirectory -prefix AD
} else {
$s = New-PSSession -computerName DC01WDC01
Invoke-command { import-module ActiveDirectory } -session $s
Export-PSSession -session $s -commandname *-AD* -outputmodule ActiveDirectory -allowclobber
Import-Module ActiveDirectory -prefix AD
Remove-PSSession -session $s
}
$servers = Get-ADADComputer -Filter {(Name -Like "RDS*")} | Select -Expand Name
foreach ($server in $servers) {
Write-Host "Working on $server" -ForegroundColor Magenta
if(!(Test-Connection -ComputerName $server -Count 1 -quiet)) {
Write-Warning "$server : Offline"
Continue
}
if ($update -eq $true) {
Invoke-Command -ComputerName $server -ArgumentList $newValue -ScriptBlock {
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\SubSystems" -Name "Windows" -Value $args[0]
$result = Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\SubSystems" | SELECT Windows
if ($result.Windows -like "*SharedSection=1024,20480,2048*") {
Write-Host "Successfully reverted Desktop Heap Limit to 2048" -ForegroundColor Green
} else {
Write-Warning "Update to registry value unsuccessful on $env:ComputerName"
}
}
} elseif ($update -eq $false) {
Invoke-Command -ComputerName $server -ArgumentList $origValue -ScriptBlock {
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\SubSystems" -Name "Windows" -Value $args[0]
$result = Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\SubSystems" | SELECT Windows
if ($result.Windows -like "*SharedSection=1024,20480,768*") {
Write-Host "Successfully reverted Desktop Heap Limit to 768" -ForegroundColor Green
} else {
Write-Warning "Update to registry value unsuccessful on $env:ComputerName"
}
}
}
}