87 lines
3.8 KiB
PowerShell
87 lines
3.8 KiB
PowerShell
|
function New-MachineConfigConnectionString {
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
Create a new connectionStrings node with default values in machine.config
|
||
|
|
||
|
.PARAMETER Use64Bit
|
||
|
Whether to modify the 64-bit machine.config or the 32-bit machine.config
|
||
|
#>
|
||
|
[CmdletBinding()]
|
||
|
Param(
|
||
|
[bool]$Use64Bit = $true
|
||
|
)
|
||
|
|
||
|
$logLead = Get-LogLeadName
|
||
|
|
||
|
$mcPath = Get-DotNetConfigPath $use64Bit
|
||
|
$bitness = if ($Use64Bit) { "64" } else { "32" }
|
||
|
Write-Host ("$logLead : Checking ConnectionString Section in {0}-bit machine.config" -f $bitness)
|
||
|
|
||
|
[XML]$machineConfig = Read-MachineConfig $Use64Bit
|
||
|
[System.Xml.XmlElement]$configRoot = $machineConfig.configuration
|
||
|
$machineConfigIsDirty = $false
|
||
|
|
||
|
Write-Verbose "$logLead : Looking for AlkamiMaster connectionString node"
|
||
|
$masterNodes = $configRoot.SelectNodes("//connectionStrings/add[@name='AlkamiMaster']")
|
||
|
if ($null -eq $masterNodes -or $masterNodes.Count -eq 0) {
|
||
|
Write-Host "$logLead : AlkamiMaster Element not Found"
|
||
|
$connectionStringNode = $configRoot.SelectSingleNode("//connectionStrings")
|
||
|
if ($null -eq $connectionStringNode) {
|
||
|
Write-Output ("$logLead : Creating connectionStrings Element")
|
||
|
# Create the Connection String Element
|
||
|
$csElement = $machineConfig.CreateElement("connectionStrings")
|
||
|
} else {
|
||
|
# Select the Connection String Element
|
||
|
Write-Verbose "$logLead : connectionStrings Element Found"
|
||
|
$csElement = $machineConfig.SelectSingleNode("//connectionStrings")
|
||
|
}
|
||
|
|
||
|
Write-Host "$logLead : Creating AlkamiMaster Element"
|
||
|
$csKey = $machineConfig.CreateElement("add")
|
||
|
$csName = $machineConfig.CreateAttribute("name")
|
||
|
$csName.Value = "AlkamiMaster"
|
||
|
|
||
|
$csString = $machineConfig.CreateAttribute("connectionString")
|
||
|
|
||
|
if ($masterConnectionString -ne "REPLACEME") {
|
||
|
$csString.Value = $masterConnectionString
|
||
|
} else {
|
||
|
Write-Warning "$logLead : The master connection string value is still set to REPLACEME. It will be added with an empty value and must be updated manually."
|
||
|
$csString.Value = ""
|
||
|
}
|
||
|
|
||
|
$csProvider = $machineConfig.CreateAttribute("providerName")
|
||
|
$csProvider.Value = "System.Data.SqlClient"
|
||
|
|
||
|
$csKey.Attributes.Append($csName) | Out-Null
|
||
|
$csKey.Attributes.Append($csString) | Out-Null
|
||
|
$csKey.Attributes.Append($csProvider) | Out-Null
|
||
|
|
||
|
$csElement.AppendChild($csKey) | Out-Null
|
||
|
$configRoot.AppendChild($csElement) | Out-Null
|
||
|
$machineConfigIsDirty = $true
|
||
|
} else {
|
||
|
# Verify the Connection String Value
|
||
|
$alkamiNode = $configRoot.SelectNodes("//add[@name='AlkamiMaster']")
|
||
|
[System.Xml.XmlAttribute]$connectionString = ($alkamiNode.Attributes | Where-Object { $_.Name -eq "connectionString" } | Select-Object -First 1)
|
||
|
|
||
|
Write-Verbose "$logLead : Checking the AlkamiMaster connectionString value"
|
||
|
if ($connectionString.Value -ne $masterConnectionString -and $masterConnectionString -ne "REPLACEME") {
|
||
|
Write-Host "$logLead : Updating the AlkamiMaster connectionString value"
|
||
|
# Set the Connection String Value
|
||
|
$connectionString.Value = $masterConnectionString
|
||
|
$machineConfigIsDirty = $true
|
||
|
} elseif ($masterConnectionString -eq "REPLACEME") {
|
||
|
Write-Warning "$logLead : The master connection string value is still set to REPLACEME. The value will not be updated"
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if ($machineConfigIsDirty) {
|
||
|
Write-Host "$logLead : Saving Modified machine.config"
|
||
|
$machineConfig.Save($mcPath)
|
||
|
} else {
|
||
|
Write-Host "$logLead : No changes required to the machine.config"
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Set-Alias -name Create-MachineConfigConnectionString -value New-MachineConfigConnectionString;
|