Created
April 3, 2016 15:11
-
-
Save WilliamBerryiii/2a5194cd1dfe53bb7ebd88f49e831f31 to your computer and use it in GitHub Desktop.
Setting a connection string password w/ password prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Set-ConnectionStringPassword ($xml, $dbName, $dbUser, $connectionStringTemplate) { | |
$connString = New-Object System.Data.Common.DbConnectionStringBuilder | |
$conStringNodeSelector = $connectionStringTemplate -f $dbName | |
$connectionStringString = $xml.SelectSingleNode($conStringNodeSelector).connectionString | |
$connString.set_ConnectionString($connectionStringString) | |
# we might be using integrated security if so dont ask for pass | |
if(![string]::IsNullOrEmpty($connString.password)){ | |
$prompt = 'What is the {0} password for user {1}?' -f $dbname, $($connString['$dbuser']) | |
$SecurePassword = Read-Host $prompt -AsSecureString | |
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword) | |
$connString.password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) | |
} | |
$xml.SelectSingleNode($conStringNodeSelector).connectionString = $connString.ConnectionString | |
return $xml | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[xml] $xml = get-content $webConfigFile | |
$connectionStringTemplate = "/configuration/connectionStrings/add[@name='{0}']" | |
$xml = Set-ConnectionStringPass $xml 'ASPNETDB' 'Application User Name' $connectionStringTemplate | |
$xml.Save($webConfigFile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment