Muokkaa

Jaa


Azure PowerShell samples for Azure SQL Database

Applies to: Azure SQL Database

Azure SQL Database enables you to configure your databases, and pools by using Azure PowerShell.

If you don't have an Azure subscription, create an Azure free account before you begin.

Use Azure Cloud Shell

Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article, without having to install anything on your local environment.

To start Azure Cloud Shell:

Option Example/Link
Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell. Screenshot that shows an example of Try It for Azure Cloud Shell.
Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. Screenshot that shows how to launch Cloud Shell in a new window.
Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. Screenshot that shows the Cloud Shell button in the Azure portal

To run the code in this article in Azure Cloud Shell:

  1. Start Cloud Shell.

  2. Select the Copy button on a code block to copy the code.

  3. Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux, or by selecting Cmd+Shift+V on macOS.

  4. Select Enter to run the code.

If you choose to install and use the PowerShell locally, this tutorial requires AZ PowerShell 1.4.0 or later. If you need to upgrade, see Install Azure PowerShell module. If you are running PowerShell locally, you also need to run Connect-AzAccount to create a connection with Azure.

Samples

The following table includes links to sample Azure PowerShell scripts for Azure SQL Database.

Link Description
Create and configure single databases and elastic pools
Create a single database and configure a server-level firewall rule This PowerShell script creates a single database and configures a server-level IP firewall rule.
Create elastic pools and move pooled databases This PowerShell script creates elastic pools, moves pooled databases, and changes compute sizes.
Configure geo-replication and failover
Configure and fail over a single database using active geo-replication This PowerShell script configures active geo-replication for a single database and fails it over to the secondary replica.
Configure and fail over a pooled database using active geo-replication This PowerShell script configures active geo-replication for a database in an elastic pool and fails it over to the secondary replica.
Configure a failover group
Configure a failover group for a single database This PowerShell script creates a database and a failover group, adds the database to the failover group, and tests failover to the secondary server.
Configure a failover group for an elastic pool This PowerShell script creates a database, adds it to an elastic pool, adds the elastic pool to the failover group, and tests failover to the secondary server.
Scale a single database and an elastic pool
Scale a single database This PowerShell script monitors the performance metrics of a single database, scales it to a higher compute size, and creates an alert rule on one of the performance metrics.
Scale an elastic pool This PowerShell script monitors the performance metrics of an elastic pool, scales it to a higher compute size, and creates an alert rule on one of the performance metrics.
Restore, copy, and import a database
Restore a database This PowerShell script restores a database from a geo-redundant backup and restores a deleted database to the latest backup.
Copy a database to a new server This PowerShell script creates a copy of an existing database in a new server.
Import a database from a bacpac file This PowerShell script imports a database into Azure SQL Database from a bacpac file.
Sync data between databases
Sync data between databases This PowerShell script configures Data Sync to sync between multiple databases in Azure SQL Database.
Sync data between SQL Database and SQL Server on-premises This PowerShell script configures Data Sync to sync between a database in Azure SQL Database and a SQL Server on-premises database.
Update the SQL Data Sync sync schema This PowerShell script adds or removes items from the Data Sync sync schema.

Learn more about the Single-database Azure PowerShell API.

The examples listed on this page use az.sql PowerShell cmdlets to create and manage Azure SQL resources. Additional cmdlets for running queries and performing many database tasks are located in SqlServer PowerShell cmdlets. For more information, see SQL Server PowerShell.