param( $dotNetVersion = "v4.0.30319",
$machineAppSettings,
$machineConnectionStrings,
$proxyAddress,
$bypassList
)
function SafeList($list)
{
if ($list -eq $null)
{
return @()
}
return $list
}
function Is64Bit
{
#[IntPtr]::Size -eq 8
}
function GetDotNetConfigPath($fileName, $is64Bit){
if ($is64Bit){
return "$env:windir\Microsoft.NET\Framework64\$dotNetVersion\Config\$fileName"
} else{
return "$env:windir\Microsoft.NET\Framework\$dotNetVersion\Config\$fileName"
}
}
function CreateAppSettingsFileIfNotExists($is64Bit){
$appSettingsFile = GetDotNetConfigPath "appSettings.config" $is64Bit
if ((Test-Path -path $appSettingsFile) -ne $True) {
$template = "<?xml version=`"1.0`"?>
<appSettings>
</appSettings>
"
$template | Out-File $appSettingsFile
}
}
function CreateConnectionStringsFileIfNotExists($is64Bit){
$appSettingsFile = GetDotNetConfigPath "connectionStrings.config" $is64Bit
if ((Test-Path -path $appSettingsFile) -ne $True) {
$template = "<?xml version=`"1.0`"?>
<connectionStrings>
</connectionStrings>
"
$template | Out-File $appSettingsFile
}
}
function GetMachineConfigFile($is64Bit)
{
return GetDotNetConfigPath "machine.config" $is64Bit
}
function CreateConnectionStringSectionIfNotExists($is64Bit){
$inputfile = GetMachineConfigFile $is64Bit
[xml]$xml = Get-Content $inputfile
# Remove default "LocalSqlServer" standard build string if it exists
$localSqlServerNode = $xml.configuration.SelectSingleNode("//connectionStrings/add[@name='LocalSqlServer']")
if ($localSqlServerNode -ne $null){
$xml.configuration.RemoveChild($xml.configuration.connectionStrings)
#[void]$localSqlServerNode.ParentNode.RemoveChild($localSqlServerNode)
$xml.Save($inputfile)
}
$connectionStrings = $xml.configuration.connectionStrings
if ($connectionStrings -eq $null)
{
$gc = $xml.CreateElement("connectionStrings")
$gc.SetAttribute("configSource", "connectionStrings.config")
$xml.configuration.AppendChild($gc)
$xml.Save($inputfile)
}
}
function CreateAppSettingsSectionIfNotExists($is64Bit){
$inputfile = GetMachineConfigFile $is64Bit
[xml]$xml = Get-Content $inputfile
$settings = $xml.configuration.appSettings
if ($settings -eq $null)
{
$gc = $xml.CreateElement("appSettings")
$gc.SetAttribute("configSource", "appSettings.config")
$xml.configuration.AppendChild($gc)
$xml.Save($inputfile)
}
}
function CreateMachineConfigSections($is64Bit){
CreateConnectionStringSectionIfNotExists $is64Bit
CreateAppSettingsSectionIfNotExists $is64Bit
}
function UpdateAppSettings($is64Bit){
if (![string]::IsNullOrEmpty($machineAppSettings)){
$appSettingsFile = GetDotNetConfigPath("appSettings.config",$is64Bit)
$appSettings = SafeList($machineAppSettings)
$xml = [xml](get-content($appSettingsFile))
#$xml.Save($appSettingsFile + ".old")
$appSettingsSection = $xml.SelectSingleNode("//appSettings");
foreach ($updatedSetting in $appSettings)
{
$currentSetting = $appSettingsSection.SelectSingleNode("add[@key='{0}']" -f $updatedSetting.key);
if ($currentSetting -eq $null)
{
Write-Host "Creating setting: $($updatedSetting.key)"
$gc = $xml.CreateElement("add")
$gc.SetAttribute("key", $updatedSetting.key)
$gc.SetAttribute("value", $updatedSetting.value)
$appSettingsSection.AppendChild($gc)
$xml.Save($appSettingsFile)
} else {
if ($currentSetting.value -ne $updatedSetting.value){
$currentSetting.Value = $updatedSetting.value
$xml.Save($appSettingsFile)
}
}
}
} else{
Write-Host "No application settings found."
}
}
function UpdateConnectionStrings(){
if (![string]::IsNullOrEmpty($machineConnectionStrings)){
$configFile = GetDotNetConfigPath "connectionStrings.config" $is64Bit
$appSettings = SafeList($machineConnectionStrings)
$xml = [xml](get-content($configFile))
#$xml.Save($appSettingsFile + ".old")
$appSettingsSection = $xml.SelectSingleNode("//connectionStrings");
foreach ($updatedSetting in $appSettings)
{
$currentSetting = $appSettingsSection.SelectSingleNode("add[@name='{0}']" -f $updatedSetting.name);
if ($currentSetting -eq $null)
{
Write-Host "Creating connection string: $($updatedSetting.name)"
$gc = $xml.CreateElement("add")
$gc.SetAttribute("name", $updatedSetting.name)
$gc.SetAttribute("connectionString", $updatedSetting.connectionString)
$gc.SetAttribute("providerName", $updatedSetting.providerName)
$appSettingsSection.AppendChild($gc)
$xml.Save($configFile)
} else {
if ($currentSetting.connectionString -ne $updatedSetting.connectionString){
$currentSetting.connectionString = $updatedSetting.connectionString
$xml.Save($configFile)
}
}
}
} else {
Write-Host "No connection strings to update"
}
}
function CreateProxySettingsSectionIfNotExists($is64Bit, $proxyAddress, $bypassList)
{
$inputfile = GetMachineConfigFile $is64Bit
[xml]$xml = Get-Content $inputfile
$settings = $xml.configuration."system.net"
if ($settings -eq $null)
{
$sn = $xml.CreateElement("system.net")
$df = $xml.CreateElement("defaultProxy")
if ([string]::IsNullOrEmpty($proxyAddress))
{
$df.SetAttribute("enabled", "false")
}
else
{
$df.SetAttribute("enabled", "true")
}
$proxyElement = $xml.CreateElement("proxy")
$proxyElement.SetAttribute("proxyaddress", $proxyAddress)
$bypassListElements = $xml.CreateElement("bypasslist")
$bypassList = $bypassList.Split(",")
foreach ($element in $bypassList)
{
$add1 = $xml.CreateElement("add")
$add1.SetAttribute("address", $element)
$bypassListElements.AppendChild($add1)
}
$df.AppendChild($proxyElement)
$df.AppendChild($bypassListElements)
$sn.AppendChild($df)
$xml.configuration.AppendChild($sn)
$xml.Save($inputfile)
}
}
function Main (){
# 64bit
CreateConnectionStringsFileIfNotExists $true
CreateAppSettingsFileIfNotExists $true
CreateMachineConfigSections $true
CreateProxySettingsSectionIfNotExists $true $proxyAddress $bypassList
#32bit
CreateConnectionStringsFileIfNotExists $false
CreateAppSettingsFileIfNotExists $false
CreateMachineConfigSections $false
CreateProxySettingsSectionIfNotExists $false $proxyAddress $bypassList
#UpdateAppSettings
#UpdateConnectionStrings
}
Main