无法获取传递给Powershell异步作业的参数值(scriptblock)

时间:2019-01-04 15:20:30

标签: powershell asynchronous start-job scriptblock azure-rm

我正在使用Powershell脚本备份我的Azure SQL数据库,然后将其缩减为标准层。由于缩减数据库的时间很长,因此可以将其移至异步过程,并且一旦完成,就可以从主脚本中回溯其状态。

尝试在计算机上本地运行脚本代码时,我可以在scriptblock中获取参数值,但是,当将此脚本作为任务添加到我的发行版定义中时,它无法通过传递的值形成查询,并且直接在日志中显示脚本块内容。

主要议程是能够在尽可能短的时间内将数据库备份到标准层。对此采取任何新方法也将有所帮助。

Param
(
    [String]$ResourceGroupName,
    [String]$Servername,
    [String]$DatabaseName,
    [String]$Edition,
    [String]$NewPricingTier
)

$CopyDatabase = ($Databasename +'_Bkp')
try
{    
    #Take a backup of current database snapshot
    Write-Host ("Creating Backup " + $CopyDatabase)
    $NewCopyMsgdb = New-AzureRmSqlDatabaseCopy -ResourceGroupName $ResourceGroupName -ServerName $ServerName -DatabaseName $DatabaseName -CopyDatabaseName $CopyDatabase
 try
    {
        if ($NewCopyMsgdb  -ne $null)
        {
            Write-Host "Database copied successfully"
            Write-Host "----------------------------"

            Start-Job -Name DowngradePricingTier -ScriptBlock {
                #Adjust the pricing to lower level that was needed
                Write-Host "Changing the Pricing Tier through async job "
                Set-AzureRmSqlDatabase -DatabaseName $Using:CopyDatabase -ServerName $Using:ServerName -ResourceGroupName $Using:ResourceGroupName -Edition $Using:Edition -RequestedServiceObjectiveName $Using:NewPricingTier
            }
        }
    }
    catch
    {
        Write-Host "Unable to change the pricing tier due to the following error :" -ForegroundColor Red
        write-host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
        write-host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red 
    }

    #Display details for all jobs
    Get-Job -Name DowngradePricingTier | Wait-Job  | Receive-Job  | Remove-Job -Force
}
catch
{
    Write-Host "Database has not been copied successfully"
}

任务日志文件中生成的输出 Output in the logs Job output details

0 个答案:

没有答案