Skip to content

Instantly share code, notes, and snippets.

@troyhunt
Created January 22, 2015 13:12
Show Gist options
  • Save troyhunt/1cd6f9a55150b4e11002 to your computer and use it in GitHub Desktop.
Save troyhunt/1cd6f9a55150b4e11002 to your computer and use it in GitHub Desktop.
Full script for provisioning all the resources I typically use for a new Azure website and database back end
# Per project params
$projectName = "TroyPSTest"
$hostName = "troypstest.troyhunt.com"
# Environment constants
$location = "West US"
$webResourceGroup = "Default-Web-WestUS"
$webHostingPlan = "DefaultServerFarm"
$dbResourceGroup = "Default-SQL-WestUS"
$dbServerName = "snyb5o1pxk"
$dbServerLogin = "troyhunt"
$dbServerPassword = "P@ssw0rd"
$apiVersion = "2014-04-01"
# Computed vars
$dbName = $projectName + "Db"
$dbLoginName = $dbName + "Login"
$dbPassword = [guid]::NewGuid()
$dbUserName = $dbName + "User"
# Make sure we kick off in service management mode
Switch-AzureMode AzureServiceManagement
# Create the site in the appropriate location
New-AzureWebSite $projectName -Location $location
# Turn off PHP
Set-AzureWebsite -Name $projectName -PhpVersion Off
# Add a host name (only if you've already put the CNAME on the domain)
#Set-AzureWebsite -Name $projectName -HostNames @($hostName)
# Switch over to the resource manager
Switch-AzureMode AzureResourceManager
# Get the website and then change the SKU
$p = @{ "sku" = "Standard"; "serverFarm" = $webHostingPlan; "webHostingPlan" = $webHostingPlan }
Set-AzureResource -Name $projectName -ResourceGroupName Default-Web-WestUS -ResourceType Microsoft.Web/sites -ApiVersion $apiVersion -PropertyObject $p
# Switch over to the resource manager
Switch-AzureMode AzureServiceManagement
# Add a deployment slot for staging (this has to be done after setting the SKU as you can't add deployment slots under the default "free" SKU)
New-AzureWebsite $projectName -Location $location -Slot "Stage"
# Create a SQL DB
New-AzureSqlDatabase -ServerName $dbServerName -DatabaseName $dbName -Edition "Basic" -MaxSizeGB 2
# Switch over to the resource manager
Switch-AzureMode AzureResourceManager
# Create a tag
New-AzureTag -Name Project -Value $projectName
# Set the tag on the website
Set-AzureResource -Name $projectName -ResourceGroupName $webResourceGroup -ResourceType Microsoft.Web/sites -ApiVersion $apiVersion -PropertyObject $p -Tags @{ Name = "Project"; Value = $projectName }
# Set the tag on the DB
Set-AzureResource -Name $dbName -ResourceGroupName $dbResourceGroup -ParentResource servers/$dbServerName -ResourceType Microsoft.Sql/servers/databases -ApiVersion $apiVersion -Tags @{ Name = "Project"; Value = $projectName }
# Fire up SMO
Import-Module SQLPS -DisableNameChecking
$conn = New-Object System.Data.SqlClient.SqlConnection
$conn.ConnectionString = "Server=tcp:" + $dbServerName + ".database.windows.net;Database=master;User ID=" + $dbServerLogin + ";Password=" + $dbServerPassword + ";"
$srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" $conn
$db = $srv.Databases["master"]
# Create the login
$login = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login -ArgumentList $srv, $dbLoginName
$login.LoginType = "SqlLogin"
$login.PasswordPolicyEnforced = $false
$login.PasswordExpirationEnabled = $false
$login.Create($dbPassword)
# Add the user to the DB
$db = $srv.Databases[$dbName]
$dbUser = New-Object -TypeName Microsoft.SqlServer.Management.Smo.User -ArgumentList $db, $dbUserName
$dbUser.Login = $dbLoginName
$dbUser.Create()
# Switch over to the resource manager
Switch-AzureMode AzureServiceManagement
# Add the connection string to the website
$connStr = "Server=tcp:" + $dbServerName + ".database.windows.net,1433;Database=" + $dbName + ";User ID=" + $dbLoginName + "@" + $dbServerName + ";Password=" + $dbPassword + ";Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
$connStrInfo = New-Object Microsoft.WindowsAzure.Commands.Utilities.Websites.Services.WebEntities.ConnStringInfo
$connStrInfo.Name=$dbName
$connStrInfo.ConnectionString=$connStr
$connStrInfo.Type="SQLAzure"
$connStrSettings = (Get-AzureWebsite $projectName -Slot "production").ConnectionStrings
$connStrSettings.Add($connStrInfo)
Set-AzureWebsite -Name $projectName -Slot "production" -ConnectionStrings $connStrSettings
# Summary
"DB user is: " + $dbLoginName
"DB password is: " + $dbPassword
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment