In this article, you will learn to use PowerShell to deploy an Azure SQL database in an Azure resource group.
Prerequisites
- Installs needed
Azure PowerShell
- Signups needed
Azure Account
Install Azure PowerShell
Run the following command from an elevated PowerShell session: (Right Click on PowerShell and select Run as Administrator)
- Install-Module AzureRM -AllowClobber
PowerShell gallery is not configured as a trusted repository for PowerShellGet by default so for the first time you use the PSGallery you will see the following prompt:
If you don’t have NuGet or a version older than 2.8.5.201 of NuGet, you are prompted to download and install the latest version of NuGet.
Load the AzureRM module
Once the module is installed, you need to load the module into your PowerShell session. You should do this in a normal (non-elevated) PowerShell session. Modules are loaded using the Import-Module
cmdlet, as follows:
If you get this error run the following command first before running Import-Module .
- Set-ExecutionPolicy RemoteSigned
Log in to Azure
Now Log in to your Azure Subscription using this command.
You will get this screen pop up Log in with your credential.
Now let’s create variables to use in scripts
- # Resource name for your resources
- $resourcegroupname = "myResourceGroup"
- $location = "WestEurope"
- # The logical server name: Use a random value or replace with your own (do not capitalize)
- $servername = "server-$(Get-Random)"
- # Set login and password for your database
- # The login information for the server
- $adminlogin = "ServerAdmin"
- $password = "ChangeYourAdminPassword1"
- # The ip address range that you want to allow to access your server
- $startip = "0.0.0.0"
- $endip = "0.0.0.0"
- # The database name
- $databasename = "mySampleDatabase"
Create a resource group
A resource group is a logical container into which Azure resources are deployed and managed as a group. So let's create a resource group for that using New-AzureRmResourceGroup command.
- New-AzureRmResourceGroup -Name $resourcegroupname -Location $location
Create an Azure SQL Database logical server
A logical server contains a group of databases managed as a group. Let’s create a logical server using New-AzureRmSqlServer command.
- New-AzureRmSqlServer -ResourceGroupName $resourcegroupname `
- -ServerName $servername `
- -Location $location `
- -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminlogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))
Create an Azure SQL Database server-level firewall rule using the New-AzureRmSqlServerFirewallRule command. A server-level firewall rule allows an external application, such as SQL Server Management Studio or the SQLCMD utility to connect to a SQL database through the SQL Database service firewall. In the following example, the firewall is only opened for other Azure resources. To enable external connectivity, change the IP address to an appropriate address for your environment. To open all IP addresses, use 0.0.0.0 as the starting IP address and 255.255.255.255 as the ending address.
- New-AzureRmSqlServerFirewallRule -ResourceGroupName $resourcegroupname `
- -ServerName $servername `
- -FirewallRuleName "AllowSome" -StartIpAddress $startip -EndIpAddress $endip
Create a database in the server with sample data
Create a database with an S0 performance level (Check Pricing Tiers in the server using the New-AzureRmSqlDatabase command). The following example creates a database called mySampleDatabase
and loads the AdventureWorksLT sample data into this database.
- New-AzureRmSqlDatabase -ResourceGroupName $resourcegroupname `
- -ServerName $servername `
- -DatabaseName $databasename `
- -SampleName "AdventureWorksLT" `
- -RequestedServiceObjectiveName "S0"
In the next post, I will show you how to connect & query the database we created today.