如何使用PowerShell取消Azure SQL Server上的所有导入/导出操作

kfgdxczn  于 2023-11-18  发布在  Shell
关注(0)|答案(3)|浏览(160)

我正在尝试计算用于取消Azure SQL Server上所有挂起的导入/导出操作的PowerShell语法。我知道我可以使用Stop-AzSqlDatabaseActivity小工具,但这需要数据库名称(数据库可能尚未存在,因此管道Get-AzSqlDatabase将无法工作)。是否可以在不指定数据库的情况下仅指定服务器执行某些操作?
谢谢你,谢谢

xyhw6mcr

xyhw6mcr1#

打开新的PowerShell窗口,您也可以通过单击门户屏幕右上角的Cloud Shell按钮在Azure门户上使用Cloud Shell。
x1c 0d1x的数据
复制并粘贴以下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​


要取消导入或导出操作,您需要提供当前正在运行操作的资源组名称、服务器名称和数据库名称。

xxslljrj

xxslljrj2#

除了Stop-AzSqlDatabaseActivity小工具之外,您还可以使用**Database Operations-Cancel API**Rest API来取消导入或导出操作,您需要传递数据库名称,这是根据当前Azure文档的强制参数。

aydmsdu9

aydmsdu93#

对于那些像我一样使用Powershell核心和az-tool的人,你可以使用az sql db op list来列出数据库上的操作。
完整示例:

> 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

字符串

相关问题