powershell:如何在一个序列中运行多个dacservice.deploy调用

powershell:如何在一个序列中运行多个dacservice.deploy调用

问题描述:

我必须运行dacservice.deploy以将数据库从版本$from更新到版本$to。这是我的PS脚本:powershell:如何在一个序列中运行多个dacservice.deploy调用

#deploy 
For ($i=$from; $i -le $to; $i++) {  
    $dacpacFileName = $dacpacPathName + $i + '.dacpac' 

    Write-Output "-----------------------------------------------------------------------" 
    Write-Output "Deploying... $dacpacFileName to $databaseServerName\$targetDatabaseName" 
    Write-Output "-----------------------------------------------------------------------" 

    #dacpac  
    $dp = [Microsoft.SqlServer.Dac.DacPackage]::Load($dacpacFileName)  

    #options 
    $options = New-Object Microsoft.SqlServer.Dac.DacDeployOptions 
    $options.GenerateSmartDefaults="True" 
    $options.BlockOnPossibleDataLoss="False" 
    $options.IncludeTransactionalScripts = "True" 

    #deploy 
    $dacService.Deploy($dp, $targetDatabaseName, $upgradeExisting, $options)  
} 

当前dacService.Deploy由于某些原因正在并行运行。

enter image description here

反正问它运行一个接一个?

PS:我可以做sqlPackage.exe

#deploy 
For ($i=$from; $i -le $to; $i++) {  
    $dacpacFileName = $dacpacPathName + $i + '.dacpac' 

    Write-Output "-----------------------------------------------------------------------" 
    Write-Output "Deploying... $dacpacFileName to $databaseServerName\$targetDatabaseName" 
    Write-Output "-----------------------------------------------------------------------" 

    & $sqlPackageExePath /a:Publish /sf:$dacpacFileName /tdn:$targetDatabaseName /TargetServerName:$databaseServerName /p:GenerateSmartDefaults=true 

    Write-host "Finished" 
} 

MSDN文档在这方面有点轻,但部署方法是异步的方法完成,但操作仍在进行中。

您需要连接“DacServices.ProgressChanged”事件并查看传入的事件参数的状态 - 但请注意,我非常肯定您会为每个阶段获取几条“完整”消息,因此您需要还可以查看消息何时完成以及错误是否失败。

ed