gpt4 book ai didi

azure - 如何判断 New-AzSqlDatabaseImport 何时完成?

转载 作者:行者123 更新时间:2023-12-02 07:44:25 27 4
gpt4 key购买 nike

我正在尝试将 .bacpack 作为 DevOps 管道的一部分导入到新的 Azure 数据库中。我有一个确实执行导入的 PowerShell 脚本,但是在导入完成后我需要对数据库执行一些操作。这些失败是因为它们在导入完成之前运行。

我正在尝试像这样检测导入的完成情况:

$status = New-AzSqlDatabaseImport @newDbParameters

# Wait for database to deploy
$waitCount = 0
$waitDelay = 5
while (($status | Get-AzSqlDatabaseImportExportStatus).Status -eq 'InProgress') {
$waitCount++
Write-Progress -Activity "Waiting for database creation." -Status "Count: $waitCount"
Start-Sleep $waitDelay
}
Write-Progress -Activity "Database to created." -Completed

# Output results
$result = $status | Get-AzSqlDatabaseImportExportStatus
$result
if ($result.Status -eq 'Succeeded') {
Write-Progress "Database deployed." -Completed
Write-Host "Database Deployed"
}
else
{
Write-Host "Database did not deploy '$($result.Status)'-'$($result.ErrorMessage)'"
Throw $result.ErrorMessage
}

但这不起作用,因为状态可以是诸如Running, Progress = 5.00 %之类的字符串

更新:

为了添加更多细节,我在 Microsoft 提供的发布服务器上的 Azure DevOps 上运行此程序。我在 Azure PowerShell 任务 V5.* 中运行它。我在脚本中添加了几行来输出 $PSVersionTable 和 Get-Module Az.Sql。我已在下面包含了记录的输出。如您所见:PowerShell 是 V7.0.3,Az 模块是 4.7.0,Az.Sql 是 V 2.10.0。 Running, Progress = 5.00 % 肯定来自 Status 属性,而不是 StatusMessage 属性。

2020-11-07T16:01:19.1265632Z ==============================================================================
2020-11-07T16:01:21.1720968Z Generating script.
2020-11-07T16:01:21.2849730Z ========================== Starting Command Output ===========================
2020-11-07T16:01:21.3454648Z ##[command]"C:\Program Files\PowerShell\7\pwsh.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\[REDACTED].ps1'"
2020-11-07T16:01:28.2383178Z Added TLS 1.2 in session.
2020-11-07T16:01:29.0267158Z ##[command]Import-Module -Name C:\Modules\az_4.7.0\Az.Accounts\1.9.5\Az.Accounts.psd1 -Global
2020-11-07T16:01:31.4671842Z ##[command]Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue
2020-11-07T16:01:32.7361261Z ##[command]Clear-AzContext -Scope Process
2020-11-07T16:01:33.9818461Z ##[command]Connect-AzAccount -ServicePrincipal -Tenant *** -Credential System.Management.Automation.PSCredential -Environment AzureCloud @processScope
2020-11-07T16:01:35.6660673Z ##[command] Set-AzContext -SubscriptionId [REDACTED] -TenantId ***
2020-11-07T16:01:37.4234219Z PSVersionTable
2020-11-07T16:01:37.4322960Z
2020-11-07T16:01:37.4343833Z Name Value
2020-11-07T16:01:37.4345708Z ---- -----
2020-11-07T16:01:37.4346476Z PSVersion 7.0.3
2020-11-07T16:01:37.4349569Z PSEdition Core
2020-11-07T16:01:37.4350222Z GitCommitId 7.0.3
2020-11-07T16:01:37.4355758Z OS Microsoft Windows 10.0.14393
2020-11-07T16:01:37.4356853Z Platform Win32NT
2020-11-07T16:01:37.4378974Z PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
2020-11-07T16:01:37.4379723Z PSRemotingProtocolVersion 2.3
2020-11-07T16:01:37.4380231Z SerializationVersion 1.1.0.1
2020-11-07T16:01:37.4380545Z WSManStackVersion 3.0
2020-11-07T16:01:37.4387506Z Az.Sql
2020-11-07T16:01:37.4675347Z
2020-11-07T16:01:37.4682595Z Name : Az.Sql
2020-11-07T16:01:37.4684105Z Path : C:\Modules\az_4.7.0\Az.Sql\2.10.0\Az.Sql.psm1
2020-11-07T16:01:37.4689463Z Description : Microsoft Azure PowerShell - SQL service cmdlets for Azure Resource Manager in Windows PowerShell
2020-11-07T16:01:37.4690369Z and PowerShell Core.
2020-11-07T16:01:37.4690846Z
2020-11-07T16:01:37.4691418Z For more information on SQL, please visit the following:
2020-11-07T16:01:37.4693344Z https://learn.microsoft.com/azure/sql-database/
2020-11-07T16:01:37.4694862Z Guid : f088f4ab-1b59-4836-a6e0-4e14d15800c6
2020-11-07T16:01:37.4697620Z Version : 2.10.0
2020-11-07T16:01:37.4698325Z ModuleBase : C:\Modules\az_4.7.0\Az.Sql\2.10.0
2020-11-07T16:01:37.4698841Z ModuleType : Script
2020-11-07T16:01:37.4699266Z PrivateData : {PSData}
2020-11-07T16:01:37.4700897Z AccessMode : ReadWrite
2020-11-07T16:01:37.4703133Z ExportedAliases : {[Disable-AzSqlServerAdvancedThreatProtection, Disable-AzSqlServerAdvancedThreatProtection],
2020-11-07T16:01:37.4704800Z [Enable-AzSqlServerAdvancedThreatProtection, Enable-AzSqlServerAdvancedThreatProtection],
2020-11-07T16:01:37.4705980Z [Get-AzSqlDatabaseLongTermRetentionPolicy, Get-AzSqlDatabaseLongTermRetentionPolicy],
2020-11-07T16:01:37.4706882Z [Get-AzSqlInstanceTDEProtector, Get-AzSqlInstanceTDEProtector]…}
2020-11-07T16:01:37.4710468Z ExportedCmdlets : {[Add-AzSqlDatabaseToFailoverGroup, Add-AzSqlDatabaseToFailoverGroup], [Add-AzSqlElasticJobStep,
2020-11-07T16:01:37.4713706Z Add-AzSqlElasticJobStep], [Add-AzSqlElasticJobTarget, Add-AzSqlElasticJobTarget],
2020-11-07T16:01:37.4714478Z [Add-AzSqlInstanceKeyVaultKey, Add-AzSqlInstanceKeyVaultKey]…}
2020-11-07T16:01:37.4715146Z ExportedFunctions : {}
2020-11-07T16:01:37.4715380Z ExportedVariables : {}
2020-11-07T16:01:37.4715705Z NestedModules : {Microsoft.Azure.PowerShell.Cmdlets.Sql}
2020-11-07T16:01:37.4716908Z
2020-11-07T16:01:39.8406335Z
2020-11-07T16:01:39.8412106Z OperationStatusLink : https://management.azure.com/subscriptions/[REDACTED]/provider
2020-11-07T16:01:39.8414296Z s/Microsoft.Sql/locations/uksouth/importExportOperationResults/[REDACTED]
2020-11-07T16:01:39.8415688Z [REDACTED]?api-version=2020-02-02-preview
2020-11-07T16:01:39.8416460Z ErrorMessage :
2020-11-07T16:01:39.8417030Z LastModifiedTime : 11/7/2020 4:01:39 PM
2020-11-07T16:01:39.8417715Z QueuedTime : 11/7/2020 4:01:38 PM
2020-11-07T16:01:39.8418302Z RequestType : Import
2020-11-07T16:01:39.8418922Z Status : Running, Progress = 5.00 %
2020-11-07T16:01:39.8419526Z StatusMessage :
2020-11-07T16:01:39.8419842Z PrivateEndpointRequestStatus : {}
2020-11-07T16:01:39.8420140Z
2020-11-07T16:01:39.8467940Z Database did not deploy 'Running, Progress = 5.00 %'-''
2020-11-07T16:01:40.7865529Z ##[error]ScriptHalted
2020-11-07T16:01:40.9680645Z ##[error]PowerShell exited with code '1'.
2020-11-07T16:01:41.9027974Z ##[section]Finishing: Deploy baseline DB from bacpac

最佳答案

命令 Get-AzSqlDatabaseImportExportStatus 将返回对象 AzureSqlDatabaseImportExportStatusModel。该对象具有一些属性。其属性状态将返回Succeeded InProgress。其属性 StatusMessage 将返回 progress(例如 Running, Progress = 5.00 %)。更多详情请引用herehere

例如

# Set the resource group name and location for your server
$resourceGroupName = ""
$location = ""
# Set an admin login and password for your server
$adminSqlLogin = ""
$password = ""
# Set server name - the logical server name has to be unique in the system
$serverName = ""
# The sample database name
$databaseName = "myImportedDatabase"
# The storage account name and storage container name
$storageAccountName = ""
$storageContainerName = ""
# BACPAC file name
$bacpacFilename = ""

$importRequest = New-AzSqlDatabaseImport -ResourceGroupName $resourceGroupName `
-ServerName $serverName `
-DatabaseName $databaseName `
-DatabaseMaxSizeBytes "262144000" `
-StorageKeyType "StorageAccessKey" `
-StorageKey $(Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -StorageAccountName $storageAccountName).Value[0] `
-StorageUri "https://$storageaccountname.blob.core.windows.net/$storageContainerName/$bacpacFilename" `
-Edition "Standard" `
-ServiceObjectiveName "S3" `
-AdministratorLogin "$adminSqlLogin" `
-AdministratorLoginPassword $(ConvertTo-SecureString -String $password -AsPlainText -Force)
# Check import status and wait for the import to complete
# Wait for database to deploy

$waitDelay = 5
while (($importRequest | Get-AzSqlDatabaseImportExportStatus).Status -eq 'InProgress') {
($importRequest | Get-AzSqlDatabaseImportExportStatus).StatusMessage
Start-Sleep $waitDelay
}


# Output results
$result = $importRequest | Get-AzSqlDatabaseImportExportStatus
$result
if ($result.Status -eq 'Succeeded') {

Write-Host "Database Deployed"
}
else
{
Write-Host "Database did not deploy '$($result.Status)'-'$($result.ErrorMessage)'"
Throw $result.ErrorMessage
}

关于azure - 如何判断 New-AzSqlDatabaseImport 何时完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64636603/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com