使用 Fabric REST API 启动和停止 SQL 数据库镜像

适用于:✅Microsoft Fabric 中的 SQL 数据库

可以使用 Fabric REST API 启动和停止从 SQL 数据库到 Fabric 中的 OneLake 的镜像操作。 本文和示例脚本演示如何使用 PowerShell 调用 Fabric REST API 启动或停止镜像。

默认情况下,与 OneLake 的 SQL 数据库镜像始终正在运行。 在某些情况下,可能需要停止 Fabric 中 SQL 数据库的镜像。 例如,若要在现有表上启用创建聚集列索引,该索引在镜像运行时无法创建。

先决条件

在 Fabric 中停止将 SQL 数据库镜像到 OneLake

以下 PowerShell 示例停止将 SQL 数据库镜像到 Fabric 中的 OneLake。

此示例脚本使用 Connect-AzAccountaz login 的别名)来提示输入凭据。 它使用这些凭据获取用于 REST API 调用的访问令牌。 SQLCMD 使用提供给 Connect-AzAccount 的帐户的上下文。

在以下脚本中,需要提供工作区 ID 和数据库 ID。 这两者都可以在 URL 中找到。 https://powerbi.com/groups/<fabric_workspace_id>/sqldatabases/<fabric_sql_database_id>。 URL 中的第一个字符串是 Fabric 工作区 ID,第二个字符串是 SQL 数据库 ID。

  • <your workspace id> 替换为你的 Fabric 工作区 ID。 可以在 URL 中轻松找到工作区的 ID,它是浏览器窗口中 / 后两个 /groups/ 字符内的唯一字符串。
  • <your database id> 替换为在 Fabric 数据库 ID 中的 SQL 数据库。 你可以在 URL 中轻松找到数据库项的标识符,它是在浏览器窗口中/sqldatabases/之后,位于两个/字符内的唯一字符串。

此脚本演示:

  1. 使用 Get-AzAccessToken 检索访问令牌,并将其从安全字符串形式进行转换。 如果使用 PowerShell 7,ConvertFrom-SecureString 也是一个选项。
  2. 组装 API 调用。
  3. 调用 API。
Import-Module Az.Accounts

az login

$workspaceid = '<your workspace id>' # Find in the URL
$databaseid = '<your database id>' # Find in the URL

$headers = $null

# 1. Get the access token and add it to the headers

$access_token = (Get-AzAccessToken -AsSecureString -ResourceUrl https://api.fabric.microsoft.com)

$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($access_token.Token)

try {
$headers = @{ 
       Authorization = $access_token.Type + ' ' + ([System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr))
    }

$access_token.UserId -match('^[^@]+') | Out-Null

$stopMirroringUri = "https://api.fabric.microsoft.com/v1/workspaces/$workspaceid/sqlDatabases/$databaseid/stopMirroring"

$parameters = @{
        Method="Post"
        Headers=$headers
        Uri = $stopMirroringUri
    }

Invoke-RestMethod @parameters -ErrorAction Stop

 } finally {
    # The following lines ensure that sensitive data is not left in memory.
    $headers = [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
}

开始将 SQL 数据库镜像到 Fabric 中的 OneLake

以下 PowerShell 示例开始将 SQL 数据库镜像到 Fabric 中的 OneLake。

此示例脚本使用 Connect-AzAccountaz login 的别名)来提示输入凭据。 它使用这些凭据获取用于 REST API 调用的访问令牌。 SQLCMD 使用提供给 Connect-AzAccount 的帐户的上下文。

在以下脚本中,将 <your workspace id> 替换为你的 Fabric 工作区 ID。 可以在 URL 中轻松找到工作区的 ID,它是浏览器窗口中 / 后两个 /groups/ 字符内的唯一字符串。 例如,11aa111-a11a-1111-1abc-aa1111aaaa 中的 https://fabric.microsoft.com/groups/11aa111-a11a-1111-1abc-aa1111aaaa/

此脚本演示:

  1. 使用 Get-AzAccessToken 检索访问令牌,并将其从安全字符串形式进行转换。 如果使用 PowerShell 7,ConvertFrom-SecureString 也是一个选项。
  2. 组装 API 调用。
  3. 调用 API
Import-Module Az.Accounts

az login

$workspaceid = '<your workspace id>' # Find in the URL
$databaseid = '<your database id>' # Find in the URL

$headers = $null

# 1. Get the access token and add it to the headers

$access_token = (Get-AzAccessToken -AsSecureString -ResourceUrl https://api.fabric.microsoft.com)

$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($access_token.Token)

try {
$headers = @{ 
       Authorization = $access_token.Type + ' ' + ([System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr))
    }

$access_token.UserId -match('^[^@]+') | Out-Null

$startMirroringUri = "https://api.fabric.microsoft.com/v1/workspaces/$workspaceid/sqlDatabases/$databaseid/startMirroring"

$parameters = @{
        Method="Post"
        Headers=$headers
        Uri = $startMirroringUri
    }

Invoke-RestMethod @parameters -ErrorAction Stop

 } finally {
    # The following lines ensure that sensitive data is not left in memory.
    $headers = [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
}