Introduction
In this article, we will explore how to work with SQL Server in the Azure Portal using PowerShell. We will utilize the Cloud Shell, which allows us to work with either bash or PowerShell. For the purpose of this article, we will focus on PowerShell. We will cover some basic commands in PowerShell and then proceed to create Azure SQL Servers, Azure Resource Groups, and Azure SQL Databases.
Requirements
- An Azure Account
Getting started
To access the Cloud Shell, click on the >_ icon located at the top right of the Azure Portal. By default, it opens the PowerShell. You can check the PowerShell version by using the following cmdlet:
$PSVersionTable.PSVersion
This will display the PowerShell version information.
To get the current date in PowerShell, you can use the get-date
function. For example:
get-date
This will display the current date and time.
If you want to get just the time, you can store the date in a variable. The following example stores the current date in a variable named time
:
$time = get-date
You can use the ToShortTimeString
function to show the time only:
$time.ToShortTimeString()
This will display the current time.
Creating a Resource Group
Azure resources can be grouped and require a group to be created. To create a Resource Group in Azure, you can use the following cmdlet:
New-AzureRmResourceGroup -Name sqlcentralgroup -Location "Central US"
If the resource group is created successfully, a confirmation message will be displayed.
You can verify that the resource group is created with the following cmdlet:
Get-AzureRmResourceGroup -Name sqlcentralgroup
This will display the details of the resource group.
Creating an Azure SQL Server
Once the resource group is created, you can proceed to create an Azure SQL Server. The following cmdlet can be used:
New-AzureRmSqlServer -ResourceGroupName "sqlcentralgroup" -Location "Central US" -ServerName "sqlcentralsrv" -ServerVersion "12.0" -SqlAdministratorCredentials (Get-Credential)
The Get-Credential
command will prompt you to enter a username and a strong password for the Azure SQL Server administrator.
If everything is successful, the Azure SQL Server will be created.
You can verify the Azure SQL Servers created using the following cmdlet:
Get-AzureRmSqlServer -ResourceGroupName "sqlcentralgroup"
This will display the details of the Azure SQL Server.
Creating an Azure SQL Database
With the Azure SQL Server and the resource group in place, you can now create an Azure SQL Database. Use the following cmdlet:
New-AzureRmSqlDatabase -ResourceGroupName "sqlcentralgroup" -ServerName "sqlcentralsrv" -DatabaseName "sqlcentraldb" -RequestedServiceObjectiveName "DW100"
This command creates a database named sqlcentraldb
. You need to specify the Resource Group Name, the SQL Azure Server Name, and the Requested Service Objective Name, which determines the performance tier of the database.
You can verify the database information with the following cmdlet:
Get-AzureRmSqlDatabase -ResourceGroupName "sqlcentralgroup" -ServerName "sqlcentralsrv"
Now you have your Azure SQL Database ready to be used.
Conclusion
In this article, we have learned how to work with SQL Server in the Azure Portal using PowerShell. We have seen how to perform basic tasks such as getting the current date and time, creating a resource group, creating an Azure SQL Server, and creating an Azure SQL Database. PowerShell provides a powerful and efficient way to manage SQL Server in the Azure environment.