ps/Modules/Alkami.PowerShell.Common/Public/Set-SystemWebSettings.ps1
2023-05-30 22:51:22 -07:00

107 lines
4.4 KiB
PowerShell

function Set-SystemWebSettings {
<#
.SYNOPSIS
Sets system.web settings to recommended values in the specified config file. Filepath defaults to the 64 bit machine config.
#>
param (
[Parameter(Mandatory = $false)]
[Alias("Path")]
[string]$filePath = (Get-DotNetConfigPath -use64Bit $true)
)
$XmlNodeValues = @(
@{XPath = "//system.web/processModel"; AttributeName = "autoConfig"; AttributeValue = "false" },
@{XPath = "//system.web/processModel"; AttributeName = "maxWorkerThreads"; AttributeValue = "400" },
@{XPath = "//system.web/processModel"; AttributeName = "maxIoThreads"; AttributeValue = "400" },
@{XPath = "//system.web/processModel"; AttributeName = "minWorkerThreads"; AttributeValue = "100" },
@{XPath = "//system.web/processModel"; AttributeName = "minIoThreads"; AttributeValue = "100" },
@{XPath = "//system.web/httpRuntime"; AttributeName = "minFreeThreads"; AttributeValue = "704" },
@{XPath = "//system.web/httpRuntime"; AttributeName = "minLocalRequestFreeThreads"; AttributeValue = "608" }
)
$logLead = (Get-LogLeadName);
$dirty = $false;
if (!(Test-Path -PathType Leaf -Path $filePath))
{
Write-Warning ("$logLead : Could not find a file at {0}. Execution cannot continue" -f $filePath);
return $null;
}
Write-Verbose "$logLead : Reading Config file at $filePath";
$xml = Read-XMLFile $filePath;
if(!$xml)
{
throw "$logLead : Config at $filePath could not be converted to xml.";
}
Write-Verbose "$logLead : Ensuring configuration and connectionStrings nodes exist...";
if(!$xml.configuration){
[void]$xml.AppendChild($xml.CreateNode("element","configuration", $null))
$dirty = $true;
} else {
Write-Host "$logLead : Found configuration node"
}
if(!$xml.configuration.'system.web'){
[void]$xml.SelectSingleNode("configuration").AppendChild($xml.CreateElement("system.web"));
$dirty = $true;
} else {
Write-Verbose "$logLead : Found system.web node"
}
$systemWebNode = $xml.configuration.SelectSingleNode('system.web')
Write-Verbose "$logLead : Looking for an existing 'processModel' node."
$processModel = $xml.configuration.'system.web'.SelectSingleNode("processModel");
$foundSetting = $false;
if($processModel)
{
$foundSetting = $true;
Write-Verbose "$logLead : Found processModel node.";
}
if(!$foundSetting)
{
Write-Host "$logLead : Could not find processModel node, creating one.";
$processModelElement = $xml.CreateElement("processModel");
$systemWebNode.AppendChild($processModelElement);
$dirty = $true;
}
$foundSetting = $false
Write-Verbose "$logLead : Looking for an existing 'httpRuntime' node."
$httpRuntime = $xml.configuration.'system.web'.SelectSingleNode("httpRuntime");
if($httpRuntime)
{
$foundSetting = $true;
Write-Verbose "$logLead : Found httpRuntime node.";
}
if(!$foundSetting)
{
Write-Host "$logLead : Could not find httpRuntime node, creating one.";
$httpRuntimeElement = $xml.CreateElement("httpRuntime");
$systemWebNode.AppendChild($httpRuntimeElement);
$dirty = $true;
}
foreach ($node in $XmlNodeValues) {
Write-Host ("$logLead : Setting {0} to {1}" -f $node.XPath, $node.AttributeValue)
$changed = Set-XmlNodeValue $xml `
-NodeString $node.XPath `
-AttributeName $node.AttributeName `
-AttributeValue $node.AttributeValue `
-ErrorAction SilentlyContinue
if ($changed) {
$dirty = $true
}
}
if($dirty)
{
# Global variable - $utfNoBOM is an Alkami global variable to make up for a PS shortcoming.
Write-Verbose "$logLead : Saving Config to path $filePath";
Save-XMLFile $filePath $xml.OuterXml $utfNoBOM
}
else
{
Write-Verbose "$logLead : No changes were made to $filePath"
}
}