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

标签 powershell azure-sql-database azure-powershell

我正在尝试计算出用于取消 Azure SQL Server 上所有挂起的导入/导出操作的 PowerShell 语法。我知道我可以使用 Stop-AzSqlDatabaseActivity cmdlet,但这需要数据库名称(数据库可能不存在,因此管道 Get-AzSqlDatabase 将不起作用)。有什么我可以不指定数据库而只指定服务器的事情吗?

谢谢!

最佳答案

打开新的 PowerShell 窗口,您也可以通过单击门户屏幕右上角的云外壳按钮在 Azure 门户上使用云外壳。

enter image description here

复制并粘贴以下 PowerShell 代码并执行它 - 它将为当前 PowerShell session 创建一个函数

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​

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

关于powershell - 如何使用 PowerShell 取消 Azure SQL Server 上的所有导入/导出操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70848680/

相关文章:

c# - 使用 SMO 在 SQL Azure 数据库中创建表失败

c# - Azure Functions 数据库连接字符串

azure - 对 Azure 应用服务进行压力测试定期停止处理请求

azure - Powershell 脚本不会列出过期的 key 保管库证书

regex - Powershell/正则表达式 : Get 8 Characters after given String

powershell - 在PowerShell中从system.array中删除项目

计划任务中的 Powershell 模块

linux - 在 bash 脚本要求时通过命令行提供密码

azure - 如何使用 powershell 检查 azure 虚拟机上是否启用了备份

azure - 从 Azure 自动化帐户运行时 Get-AzureBlobContent 抛出错误