我正在尝试计算出用于取消 Azure SQL Server 上所有挂起的导入/导出操作的 PowerShell 语法。我知道我可以使用 Stop-AzSqlDatabaseActivity
cmdlet,但这需要数据库名称(数据库可能不存在,因此管道 Get-AzSqlDatabase
将不起作用)。有什么我可以不指定数据库而只指定服务器的事情吗?
谢谢!
最佳答案
打开新的 PowerShell 窗口,您也可以通过单击门户屏幕右上角的云外壳按钮在 Azure 门户上使用云外壳。
复制并粘贴以下 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/