Welcome to Shaun Luttin's public notebook. It contains rough, practical notes. The guiding idea is that, despite what marketing tells us, there are no experts at anything. Sharing our half-baked ideas helps everyone. We're all just muddling thru. Find out more about our work at bigfont.ca.

Configure Azure Website Connection Strings with PowerShell

Tags: azure, azure-websites, powershell

# Sign in to Azure through the UI

Add-AzureAccount 

# Inspect existing connection strings

$connStrings = (Get-AzureWebsite website-name-here).ConnectionStrings
Write-Host ($connStrings | Format-List | Out-String)

# Create new connection string
# Type must be one of MySql, SQLServer, SQLAzure, Custom

$newConnString = New-Object Microsoft.WindowsAzure.Commands.Utilities.Websites.Services.WebEntities.ConnStringInfo
$newConnString.Name = "connection-string-name-here"
$newConnString.ConnectionString "connection-string-here"
$newConnString.Type = "SqlAzure"

# Change an existing connection string by name

$connStrings.Find({ param($m) $m.Name.Equals("connection-string-name-here") }).ConnectionString = 'onnection-string-here'

# Change an existing connection string with variables

$newPassword = 'SUPER_DUPER_SECURE_PASSWORD_HERE' # single quotes for literal
$websiteName = ""
$dbName = ""
$connStringName = ""
$connString = "Data Source=tcp:abcdefghi.database.windows.net,1234;Initial Catalog=$($dbName);User [email protected];Password=$($newPassword)"

$connStrings = (Get-AzureWebsite $websiteName).ConnectionStrings
$connStrings.Find({ param($m) $m.Name.Equals($connStringName) }).ConnectionString = $connString
Set-AzureWebsite $websiteName -ConnectionStrings $connStrings

# push – BE CAREFUL THIS GOES LIVE

$connStrings.Add($newConnString)
Set-AzureWebsite website-name-here -ConnectionStrings $connStrings

See also