Azure 自动化 PowerShell 脚本运行命令两次
Azure Automation PowerShell Script runs commands twice
我有一个 Azure Automation PowerShell 脚本,每晚都会安排 运行。出于某种原因,工作流会每隔一段时间 运行 然后在 运行ning 工作流的同一实例中再次从头开始 运行ning 所有命令。我没有循环整个工作流程或任何东西,所以我不知道这是怎么发生的或为什么会发生。有什么想法吗?
代码如下:
workflow Start-SQLDatabaseBackup
{
param(
[parameter(Mandatory=$true)]
[string] $credName = 'automation',
[parameter(Mandatory=$true)]
[string] $SubscriptionName,
[parameter(Mandatory=$true)]
[string] $SQLServerName,
[parameter(Mandatory=$true)]
[string] $DatabaseName,
[parameter(Mandatory=$true)]
[string] $StorageAccountName,
[parameter(Mandatory=$true)]
[string] $ContainerName ='backup',
[parameter(Mandatory=$false)]
[string] $time
)
inlinescript {
Write-Output ("Starting Database Backup for " + $Using:DatabaseName)
$Credential = Get-AutomationPSCredential -Name $Using:credName
if($Credential)
{
Write-Output ("Found Automation Credential Asset named " + $Using:credName)
}
else
{
throw ("Could not find an Automation Credential Asset named" + $Using:credName + ". Make sure you have created one in this Automation Account." )
}
$SQLCredential = Get-AutomationPSCredential -Name '****'
if($SQLCredential)
{
Write-Output ("Found SQL Credential Asset named " + $Using:SQLcredName)
}
else
{
throw ("Could not find an SQL Credential Asset named " + $Using:SQLcredName + ". Make sure you have created one in this Automation Account.")
}
Add-AzureAccount -Credential $Credential > $null
Select-AzureSubscription -SubscriptionName $Using:SubscriptionName
if (!(Test-AzureName -Storage $Using:StorageAccountName))
{
Write-Output ("Creating Storage Account " + $Using:StorageAccountName)
New-AzureStorageAccount -StorageAccountName $Using:StorageAccountName -Location $Using:Location
}
Set-AzureSubscription -SubscriptionName $Using:SubscriptionName -CurrentStorageAccountName $Using:StorageAccountName
$SqlContext = New-AzureSqlDatabaseServerContext -ServerName $Using:SQLServerName -Credential $SQLCredential
if($SqlContext)
{
Write-Output ("Created SQL Context for " + $Using:SQLServerName)
}
else
{
throw ("Could not Create SQL Context for " + $Using:SQLServerName + ". Make sure SQL Credential Asset named" + $Using:SQLcredName + " has access to the server" )
}
$time = $Using:time
if ([string]::IsNullOrEmpty($time) )
{
$start = [System.DateTime]::UtcNow
$time = $start.ToString("yyyyMMdd_hh-mm-ss-tt")
}
$fileName = ( "{2}/{0}_{1}.bacpac" -f $Using:DatabaseName, $time, $time )
$Container = Get-AzureStorageContainer -Name $Using:ContainerName
if($Container)
{
Write-Output ("Retrieved Azure Storage Container - " + $Using:ContainerName)
}
else
{
throw ("Could not Retrieve Azure Storage Container " + $Using:ContainerName + ". Make sure the Storage Container exists. " )
}
Write-Output ("Starting Copy of " + $Using:DatabaseName)
$dbCopyName = ($Using:DatabaseName +"_copy")
$dbCopy = Start-AzureSqlDatabaseCopy -ServerName $Using:SQLServerName -DatabaseName $Using:DatabaseName -PartnerDatabase $dbCopyName
$doLoop = 1
while ($doLoop -eq 1)
{
$copyStatus = Get-AzureSqlDatabaseCopy -ServerName $Using:SQLServerName -DatabaseCopy $dbCopy -ErrorAction SilentlyContinue
if ($copyStatus -ne $null)
{
Write-Output $copyStatus.PercentComplete
Start-Sleep -s 10
}
else
{
$doLoop=0
Start-Sleep -s 10
}
}
Get-AzureSqlDatabase -ConnectionContext $SqlContext -DatabaseName $dbCopyName
Write-Output ("Starting Export of " + $dbCopyName )
$exportRequest = Start-AzureSqlDatabaseExport -SqlConnectionContext $SqlContext -StorageContainer $Container -DatabaseName $dbCopyName -BlobName $fileName
if ($exportRequest)
{
$doLoop = 1
while ($doLoop -eq 1)
{
$exportStatus = Get-AzureSqlDatabaseImportExportStatus -Request $exportRequest
if ($exportStatus.Status -eq "Completed")
{
$doLoop = 0
}
elseif ($exportStatus.Status -eq "Failed")
{
$doLoop = 0
throw ("Exporting database " + $dbCopyName + " failed")
}
else
{
Write-Output $exportStatus.Status
Start-Sleep -s 10
}
}
Write-Output ("Backup " + $fileName + " Created" )
}
else
{
throw ("Error Creating Export Request for " + $dbCopyName)
}
Write-Output ("Deleting " + $dbCopyName)
Remove-AzureSqlDatabase -ConnectionContext $SqlContext -DatabaseName $dbCopyName -Force
}
}
这是输出。您可以看到它在完成后再次从脚本顶部开始执行命令。运行
Starting Database Backup for *****
Found Automation Credential Asset named ****
Found SQL Credential Asset named
Created SQL Context for *****
Retrieved Azure Storage Container - backup
Starting Copy of ******
0
0
0
0
0
0
0
100
Starting Export of ******
Pending
Running, Progress = 0%
**Starting Database Backup for ******** < -- Starts running from the top of script again?
Found Automation Credential Asset named ****
Found SQL Credential Asset named
Created SQL Context for *******
Retrieved Azure Storage Container - backup
Starting Copy of *****
Starting Export of *****
您可能 运行 进入 Azure 自动化的 "fair share" 限制,我们卸载任何需要超过 3 小时的作业(以确保其他作业有机会 运行 ),然后从最后一个检查点恢复作业(如果 运行 书中没有检查点,就像你的情况一样,作业将从头开始恢复)。
如果您想确保您的 运行 书籍的某些部分在达到 Azure Automation 公平份额限制的情况下不会被重新运行,请确保您的 运行 书籍作业将在三小时内完成,或者在 运行本书不应重复的部分之后添加检查点(通过 Checkpoint-Workflow)。
我有一个 Azure Automation PowerShell 脚本,每晚都会安排 运行。出于某种原因,工作流会每隔一段时间 运行 然后在 运行ning 工作流的同一实例中再次从头开始 运行ning 所有命令。我没有循环整个工作流程或任何东西,所以我不知道这是怎么发生的或为什么会发生。有什么想法吗?
代码如下:
workflow Start-SQLDatabaseBackup
{
param(
[parameter(Mandatory=$true)]
[string] $credName = 'automation',
[parameter(Mandatory=$true)]
[string] $SubscriptionName,
[parameter(Mandatory=$true)]
[string] $SQLServerName,
[parameter(Mandatory=$true)]
[string] $DatabaseName,
[parameter(Mandatory=$true)]
[string] $StorageAccountName,
[parameter(Mandatory=$true)]
[string] $ContainerName ='backup',
[parameter(Mandatory=$false)]
[string] $time
)
inlinescript {
Write-Output ("Starting Database Backup for " + $Using:DatabaseName)
$Credential = Get-AutomationPSCredential -Name $Using:credName
if($Credential)
{
Write-Output ("Found Automation Credential Asset named " + $Using:credName)
}
else
{
throw ("Could not find an Automation Credential Asset named" + $Using:credName + ". Make sure you have created one in this Automation Account." )
}
$SQLCredential = Get-AutomationPSCredential -Name '****'
if($SQLCredential)
{
Write-Output ("Found SQL Credential Asset named " + $Using:SQLcredName)
}
else
{
throw ("Could not find an SQL Credential Asset named " + $Using:SQLcredName + ". Make sure you have created one in this Automation Account.")
}
Add-AzureAccount -Credential $Credential > $null
Select-AzureSubscription -SubscriptionName $Using:SubscriptionName
if (!(Test-AzureName -Storage $Using:StorageAccountName))
{
Write-Output ("Creating Storage Account " + $Using:StorageAccountName)
New-AzureStorageAccount -StorageAccountName $Using:StorageAccountName -Location $Using:Location
}
Set-AzureSubscription -SubscriptionName $Using:SubscriptionName -CurrentStorageAccountName $Using:StorageAccountName
$SqlContext = New-AzureSqlDatabaseServerContext -ServerName $Using:SQLServerName -Credential $SQLCredential
if($SqlContext)
{
Write-Output ("Created SQL Context for " + $Using:SQLServerName)
}
else
{
throw ("Could not Create SQL Context for " + $Using:SQLServerName + ". Make sure SQL Credential Asset named" + $Using:SQLcredName + " has access to the server" )
}
$time = $Using:time
if ([string]::IsNullOrEmpty($time) )
{
$start = [System.DateTime]::UtcNow
$time = $start.ToString("yyyyMMdd_hh-mm-ss-tt")
}
$fileName = ( "{2}/{0}_{1}.bacpac" -f $Using:DatabaseName, $time, $time )
$Container = Get-AzureStorageContainer -Name $Using:ContainerName
if($Container)
{
Write-Output ("Retrieved Azure Storage Container - " + $Using:ContainerName)
}
else
{
throw ("Could not Retrieve Azure Storage Container " + $Using:ContainerName + ". Make sure the Storage Container exists. " )
}
Write-Output ("Starting Copy of " + $Using:DatabaseName)
$dbCopyName = ($Using:DatabaseName +"_copy")
$dbCopy = Start-AzureSqlDatabaseCopy -ServerName $Using:SQLServerName -DatabaseName $Using:DatabaseName -PartnerDatabase $dbCopyName
$doLoop = 1
while ($doLoop -eq 1)
{
$copyStatus = Get-AzureSqlDatabaseCopy -ServerName $Using:SQLServerName -DatabaseCopy $dbCopy -ErrorAction SilentlyContinue
if ($copyStatus -ne $null)
{
Write-Output $copyStatus.PercentComplete
Start-Sleep -s 10
}
else
{
$doLoop=0
Start-Sleep -s 10
}
}
Get-AzureSqlDatabase -ConnectionContext $SqlContext -DatabaseName $dbCopyName
Write-Output ("Starting Export of " + $dbCopyName )
$exportRequest = Start-AzureSqlDatabaseExport -SqlConnectionContext $SqlContext -StorageContainer $Container -DatabaseName $dbCopyName -BlobName $fileName
if ($exportRequest)
{
$doLoop = 1
while ($doLoop -eq 1)
{
$exportStatus = Get-AzureSqlDatabaseImportExportStatus -Request $exportRequest
if ($exportStatus.Status -eq "Completed")
{
$doLoop = 0
}
elseif ($exportStatus.Status -eq "Failed")
{
$doLoop = 0
throw ("Exporting database " + $dbCopyName + " failed")
}
else
{
Write-Output $exportStatus.Status
Start-Sleep -s 10
}
}
Write-Output ("Backup " + $fileName + " Created" )
}
else
{
throw ("Error Creating Export Request for " + $dbCopyName)
}
Write-Output ("Deleting " + $dbCopyName)
Remove-AzureSqlDatabase -ConnectionContext $SqlContext -DatabaseName $dbCopyName -Force
}
}
这是输出。您可以看到它在完成后再次从脚本顶部开始执行命令。运行
Starting Database Backup for *****
Found Automation Credential Asset named ****
Found SQL Credential Asset named
Created SQL Context for *****
Retrieved Azure Storage Container - backup
Starting Copy of ******
0
0
0
0
0
0
0
100
Starting Export of ******
Pending
Running, Progress = 0%
**Starting Database Backup for ******** < -- Starts running from the top of script again?
Found Automation Credential Asset named ****
Found SQL Credential Asset named
Created SQL Context for *******
Retrieved Azure Storage Container - backup
Starting Copy of *****
Starting Export of *****
您可能 运行 进入 Azure 自动化的 "fair share" 限制,我们卸载任何需要超过 3 小时的作业(以确保其他作业有机会 运行 ),然后从最后一个检查点恢复作业(如果 运行 书中没有检查点,就像你的情况一样,作业将从头开始恢复)。
如果您想确保您的 运行 书籍的某些部分在达到 Azure Automation 公平份额限制的情况下不会被重新运行,请确保您的 运行 书籍作业将在三小时内完成,或者在 运行本书不应重复的部分之后添加检查点(通过 Checkpoint-Workflow)。