Skip to content

Instantly share code, notes, and snippets.

@WilliamBerryiii
Created April 3, 2016 15:11
Show Gist options
  • Save WilliamBerryiii/2a5194cd1dfe53bb7ebd88f49e831f31 to your computer and use it in GitHub Desktop.
Save WilliamBerryiii/2a5194cd1dfe53bb7ebd88f49e831f31 to your computer and use it in GitHub Desktop.
Setting a connection string password w/ password prompt
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
}
[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