Azure SQL Database 的 Azure PowerShell 範例Azure PowerShell samples for Azure SQL Database

Azure SQL Database 可讓您使用 Azure PowerShell 來設定資料庫、執行個體和集區。Azure SQL Database enables you to configure your databases, instances, and pools using Azure PowerShell.

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶If you don't have an Azure subscription, create a free account before you begin.

開啟 Azure Cloud ShellOpen Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,並且會透過瀏覽器來使用。Azure Cloud Shell is an interactive shell environment hosted in Azure and used through your browse. Azure Cloud Shell 可讓您使用 bashPowerShell 殼層來執行各種可與 Azure 服務搭配運作的工具。Azure Cloud Shell allows you to use either bash or PowerShell shells to run a variety of tools to work with Azure services. Azure Cloud Shell 已預先安裝一些命令,可讓您執行本文的內容,而不必在本機環境上安裝任何工具。Azure Cloud Shell comes pre-installed with the commands to allow you to run the content of this article without having to install anything on your local environment.

若要在 Azure Cloud Shell 上執行本文所包含的任何程式碼,請開啟 Cloud Shell 工作階段、使用某個程式碼區塊上的 [複製] 按鈕來複製程式碼,然後使用 Ctrl+Shift+V (在 Windows 和 Linux 上) 或 Cmd+Shift+V (在 macOS 上) 將程式碼貼到 Cloud Shell 工作階段中。To run any code contained in this article on Azure Cloud Shell, open a Cloud Shell session, use the Copy button on a code block to copy the code, and paste it into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS. 貼上的文字不會自動執行,因此請按 Enter 來執行程式碼。Pasted text is not automatically executed, so press Enter to run code.

您可以使用下列方式來啟動 Azure Cloud Shell:You can launch Azure Cloud Shell with:

選取程式碼區塊右上角的 [試試看] 。Select Try It in the upper-right corner of a code block. 這__不會__自動將文字複製到 Cloud Shell。This doesn't automatically copy text to Cloud Shell. Azure Cloud Shell 的試試看範例
在瀏覽器中開啟 shell.azure.comOpen shell.azure.com in your browser. 啟動 Azure Cloud Shell 按鈕Launch Azure Cloud Shell button
選取 Azure 入口網站右上角功能表上的 [Cloud Shell] 按鈕。Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal. Azure 入口網站中的 [Cloud Shell] 按鈕

如果選擇在本機安裝和使用 PowerShell,此教學課程需要 AZ PowerShell 1.4.0 或更新版本。If you choose to install and use the PowerShell locally, this tutorial requires AZ PowerShell 1.4.0 or later. 如果您需要升級,請參閱安裝 Azure PowerShell 模組If you need to upgrade, see Install Azure PowerShell module. 如果您在本機執行 PowerShell,則也需要執行 Connect-AzAccount 以建立與 Azure 的連線。If you are running PowerShell locally, you also need to run Connect-AzAccount to create a connection with Azure.

單一資料庫與彈性集區Single Database and Elastic pools

下表包含適用於 Azure SQL Database 之範例 Azure PowerShell 指令碼的連結。The following table includes links to sample Azure PowerShell scripts for Azure SQL Database.

建立並設定單一資料庫和彈性集區Create and configure single databases, and elastic pools
建立單一資料庫並設定資料庫伺服器防火牆規則Create a single database and configure a database server firewall rule 此 PowerShell 指令碼會建立單一 Azure SQL 資料庫,並設定伺服器層級防火牆規則。This PowerShell script creates a single Azure SQL database and configures a server-level firewall rule.
建立彈性集區並移動集區資料庫Create elastic pools and move pooled databases 此 PowerShell 指令碼會建立 Azure SQL Database 彈性集區、移動集區資料庫,並變更計算大小。This PowerShell script creates Azure SQL Database elastic pools, and moves pooled databases, and changes compute sizes.
設定異地複寫和容錯移轉Configure geo-replication and failover
使用作用中異地複寫設定單一資料庫並進行容錯移轉Configure and failover a single database using active geo-replication 此 PowerShell 指令碼會為單一 Azure SQL 資料庫 設定作用中異地複寫,並將其容錯移轉到次要複本。This PowerShell script configures active geo-replication for a single Azure SQL database and fails it over to the secondary replica.
使用作用中異地複寫設定集區資料庫並進行容錯移轉Configure and failover a pooled database using active geo-replication 此 PowerShell 指令碼會為 SQL 彈性集區中的 Azure SQL 資料庫設定作用中異地複寫,並將其容錯移轉到次要複本。This PowerShell script configures active geo-replication for an Azure SQL database in a SQL elastic pool, and fails it over to the secondary replica.
設定單一資料庫的容錯移轉群組並進行容錯移轉Configure and failover a failover group for a single database 此 PowerShell 指令碼會為 Azure SQL Database 伺服器執行個體設定容錯移轉群組,並將資料庫新增到容錯移轉群組,然後將其容錯移轉到次要伺服器。This PowerShell script configures a failover group for an Azure SQL Database server instance, adds a database to the failover group, and fails it over to the secondary server
調整單一資料庫和彈性集區Scale a single database and an elastic pool
調整單一資料庫Scale a single database 此 PowerShell 指令碼會監視 Azure SQL 資料庫的效能計量,並將其調整為較高的計算大小,然後對其中一個效能計量建立警示規則。This PowerShell script monitors the performance metrics of an Azure SQL database, scales it to a higher compute size and creates an alert rule on one of the performance metrics.
調整彈性集區Scale an elastic pool 此 PowerShell 指令碼會監視 Azure SQL Database 彈性集區的效能計量,並將其調整為較高的計算大小,然後對其中一個效能計量建立警示規則。This PowerShell script monitors the performance metrics of an Azure SQL Database elastic pool, scales it to a higher compute size, and creates an alert rule on one of the performance metrics.
稽核與威脅偵測Auditing and threat detection
設定稽核與威脅偵測Configure auditing and threat-detection 此 PowerShell 指令碼會設定 Azure SQL 資料庫的稽核與威脅偵測原則。This PowerShell script configures auditing and threat detection policies for an Azure SQL database.
還原、複製和匯入資料庫Restore, copy, and import a database
還原資料庫Restore a database 此 PowerShell 指令碼會從異地備援備份還原 Azure SQL 資料庫,並將已刪除的 Azure SQL 資料庫還原為最新的備份。This PowerShell script restores an Azure SQL database from a geo-redundant backup and restores a deleted Azure SQL database to the latest backup.
將資料庫複製到新伺服器Copy a database to new server 此 PowerShell 指令碼會在新的 Azure SQL Server 中建立現有 Azure SQL 資料庫的複本。This PowerShell script creates a copy of an existing Azure SQL database in a new Azure SQL server.
從 bacpac 檔案匯入資料庫Import a database from a bacpac file 此 PowerShell 指令碼會從 bacpac 檔案將資料庫匯入 Azure SQL Server。This PowerShell script imports a database to an Azure SQL server from a bacpac file.
同步處理資料庫之間的資料Sync data between databases
同步處理 SQL Database 之間的資料Sync data between SQL databases 此 PowerShell 指令碼會設定「資料同步」在多個 Azure SQL 資料庫之間進行同步處理。This PowerShell script configures Data Sync to sync between multiple Azure SQL databases.
內部部署 SQL Database 與 SQL Server 之間的同步資料Sync data between SQL Database and SQL Server on-premises 此 PowerShell 指令碼會設定「資料同步」在內部部署的 Azure SQL 資料庫和 SQL Server 之間進行同步處理。This PowerShell script configures Data Sync to sync between an Azure SQL database and a SQL Server on-premises database.
更新 SQL 資料同步的同步結構描述Update the SQL Data Sync sync schema 這個 PowerShell 指令碼會從資料同步的同步結構描述新增或移除項目。This PowerShell script adds or removes items from the Data Sync sync schema.

深入了解單一資料庫 Azure PowerShell APILearn more about the Single Database Azure PowerShell API.

受控執行個體Managed Instance

下表包含適用於 Azure SQL Database - 受控執行個體之範例 Azure PowerShell 指令碼的連結。The following table includes links to sample Azure PowerShell scripts for Azure SQL Database - Managed Instance.

建立及設定受控執行個體Create and configure managed instances
建立和管理受控執行個體Create and manage a Managed Instance 此 PowerShell 指令碼說明如何使用 Azure PowerShell 建立和管理受控執行個體This PowerShell script shows you how to create and manage a Managed Instance using the Azure PowerShell
使用 Azure Resource Manager 範本建立及管理受控執行個體Create and manage a Managed Instance using Azure Resource Manager template 此 PowerShell 指令碼說明如何使用 Azure PowerShell 和 Azure Resource Manager 範本建立及管理受控執行個體。This PowerShell script shows you how to create and manage a Managed Instance using the Azure PowerShell and Azure Resource Manager template.
設定透明資料加密 (TDE)Configure Transparent Data Encryption (TDE)
從 Azure Key Vault 使用自己的金鑰管理受控執行個體中的透明資料加密Manage Transparent Data Encryption in a Managed Instance using your own key from Azure Key Vault 此 PowerShell 指令碼會使用來自 Azure Key Vault 的金鑰,在Azure SQL 受控執行個體的「攜帶您自己的金鑰」案例中設定透明資料加密 (TDE)。This PowerShell script configures Transparent Data Encryption (TDE) in Bring Your Own Key scenario for Azure SQL Managed Instance, using a key from Azure Key Vault

深入了解受控執行個體 Azure PowerShell APILearn more about the Managed Instance Azure PowerShell API.

其他資源Additional resources

此頁面列出的範例會使用 Azure SQL Database Cmdlet 來建立和管理 Azure SQL 資源。The examples listed on this page use the Azure SQL Database cmdlets for creating and managing Azure SQL resources. 用來執行查詢及執行多項資料庫工作的其他 Cmdlet,位於 sqlserver 模組中。Additional cmdlets for running queries, and performing many database tasks are located in the sqlserver module. 如需詳細資訊,請參閱 SQL Server PowerShellFor more information, see SQL Server PowerShell.