我正在尝试制定用于取消 Azure SQL Server 上所有挂起的导入/导出操作的 PowerShell 语法。我知道我可以使用
Stop-AzSqlDatabaseActivity
cmdlet,但这需要一个数据库名称(数据库可能尚不存在,因此管道 Get-AzSqlDatabase
无法工作)。有什么我可以在不指定数据库而仅指定服务器的情况下执行的操作吗?
谢谢!
打开新的 PowerShell 窗口,您也可以通过单击门户屏幕右上角的 Cloud shell 按钮在 Azure 门户上使用 Cloud shell。
复制并粘贴以下 PowerShell 代码并执行它 - 它将为当前 PowerShell 会话创建一个函数
function Cancel-AzSQLImportExportOperation
{
param
(
[parameter(Mandatory=$true)][string]$ResourceGroupName
,[parameter(Mandatory=$true)][string]$ServerName
,[parameter(Mandatory=$true)][string]$DatabaseName
)
$Operation = Get-AzSqlDatabaseActivity -ResourceGroupName $ResourceGroupName -ServerName $ServerName -DatabaseName $DatabaseName | Where-Object {($_.Operation -like "Export*" -or $_.Operation -like "Import*") -and $_.State -eq "InProgress"}
if(-not [string]::IsNullOrEmpty($Operation))
{
do
{
Write-Host -ForegroundColor Cyan ("Operation " + $Operation.Operation + " with OperationID: " + $Operation.OperationId + " is now " + $Operation.State)
$UserInput = Read-Host -Prompt "Should I cancel this operation? (Y/N)"
} while($UserInput -ne "Y" -and $UserInput -ne "N")
if($UserInput -eq "Y")
{
"Canceling operation"
Stop-AzSqlDatabaseActivity -ResourceGroupName $ResourceGroupName -ServerName $ServerName -DatabaseName $DatabaseName -OperationId $Operation.OperationId
}
else
{"Exiting without cenceling the operation"}
}
else
{
"No import or export operation is now running"
}
}
Cancel-AzSQLImportExportOperation
使用该功能
Cancel-AzSQLImportExportOperation
要取消导入或导出操作,您需要提供当前运行该操作的资源组名称、服务器名称和数据库名称。
除了
Stop-AzSqlDatabaseActivity
cmdlet,您还可以使用 数据库操作-取消 API Rest API 取消导入或导出操作,您需要传递数据库名称,这是一个强制参数 根据当前 Azure 文档.
对于像我一样使用 Powershell core 和 az-cli 的人,您可以使用
az sql db op list
列出对数据库的操作。一旦找到要取消的操作,您可以使用 az sql db op cancel --name <name of the operation found with the previous command>
完整示例:
> az sql db op list --resource-group my-rg --server mssql-srv --database mydb
[
{
"databaseName": "mydb",
"description": null,
"errorCode": null,
"errorDescription": null,
"errorSeverity": null,
"estimatedCompletionTime": null,
"id": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/my-rg/providers/Microsoft.Sql/servers/mssql-srv/databases/mydb/operations/ffffffff-ffff-ffff-ffff-4ddaeccf90ff",
"isCancellable": null,
"isUserError": null,
"name": "ffffffff-ffff-ffff-ffff-4ddaeccf90ff",
"operation": "ExportDatabase",
"operationFriendlyName": "EXPORT DATABASE",
"percentComplete": 1,
"resourceGroup": "my-rg",
"serverName": "mssql-srv",
"startTime": "2023-10-13T14:34:09.427000+00:00",
"state": "InProgress",
"type": "Microsoft.Sql/servers/databases/operations"
}
]
> az sql db op cancel --resource-group my-rg --server mssql-srv --database mydb --name ffffffff-ffff-ffff-ffff-4ddaeccf90ff