Create Azure SQL Database using PowerShell cmdlets

Azure SQL Database can be created using UI in Azure Portal, using PowerShell cmdlets or by using Azure Resource Manager template written in JSON. Following PowerShell script creates Azure SQL Database using PowerShell cmdlets:

Udemy course: Improve your productivity with PowerShell

param
(
    $resourceGroup = $(throw "Resource group is required"),
    $location = $(throw "Location is required"),
    $server = $(throw "Server is required"),
    $database = $(throw "Database is required"),
    $adminLogin = $(throw "Admin login is required"),
    $adminPassword = $(throw "Admin password is required"),
    $ipAddress = $(throw "IP address is required")
)

New-AzureRmResourceGroup -Name $resourceGroup -Location $location

$securePassword = ConvertTo-SecureString -String $adminPassword -AsPlainText -Force
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminLogin, $securePassword

New-AzureRmSqlServer -ResourceGroupName $resourceGroup `
    -ServerName $server `
    -Location $location `
    -SqlAdministratorCredentials $credentials

New-AzureRmSqlServerFirewallRule -ResourceGroupName $resourceGroup `
    -ServerName $server `
    -FirewallRuleName "Default" `
    -StartIpAddress $ipAddress `
    -EndIpAddress $ipAddress

New-AzureRmSqlDatabase -ResourceGroupName $resourceGroup `
    -ServerName $server `
    -DatabaseName $database `
    -RequestedServiceObjectiveName "S0"

$result = Invoke-Sqlcmd -ServerInstance "$server.database.windows.net" `
    -Database $database `
    -Username $adminLogin `
    -Password $adminPassword `
    -Query "SELECT @@VERSION AS Version"

$result.Version

PowerShell script creates new resource group, SQL server, configures firewall and than create empty database. At the end new database is queried by simple T-SQL command.

If you are interested in PowerShell automation, take my Udemy course Improve your productivity with PowerShell.