0

我正在尝试使用遍历所有数据库的 Powershell 7-Preview 执行 Powershell 脚本(7.0)文件,使用 DACPAC 文件更新它们并在所有数据库中执行其他 SQL Server 脚本。它在没有错误时工作正常,但是,如果在执行 Dacpac 文件时出错,它会给出以下错误并停止进一步执行脚本。

##[错误]无法部署包。 ##[error] PowerShell 以代码 '1' 退出

关于我们如何在 Parallel 语句中优雅地捕获 PowerShell 中的错误并让脚本继续用于其他数据库的任何指针?Try-Catch 块似乎在这里不起作用。

我是 PowerShell 的新手。此 PowerShell 脚本是 DevOps 发布管道的一部分。

#.. Variables are defined here ..#
[string[]]$DatabaseNames = Invoke-Sqlcmd @GetDatabasesParams | select -expand name

[int]$ErrorCount = 0
$DatabaseNames | ForEach-Object -Parallel {
    try 
    {
        echo "$_ - Updating database using DACPAC."    
        dir -Path "C:\Program Files (x86)\Microsoft Visual Studio*" -Filter "SqlPackage.exe" -Recurse -ErrorAction SilentlyContinue | %{$_.FullName} {$using:SqlPackagePath /Action:Publish /tu:$using:DatabaseUsername /tp:$using:DatabasePassword /tsn:$using:ServerInstance /tdn:"$_" /sf:using:$DacpacLocation /p:BlockOnPossibleDataLoss=False}
     
        echo "$_ - Updating the scripts."
        $OngoingChangesScriptParams = @{
            "Database" = "$_"
            "ServerInstance" = "$ServerInstance"
            "Username" = "$DatabaseUsername"
            "Password" = "$DatabasePassword"
            "InputFile" ="$SystemWorkingDir\$OngoingChangesScriptLocation" 
            "OutputSqlErrors" = 1
            "QueryTimeout" = 9999
            "ConnectionTimeout" = 9999
        }

        Invoke-Sqlcmd @OngoingChangesScriptParams       
    }
    catch {
        $ErrorCount++
        echo "Internal Error. The remaining databases will still be processed."
        echo $_.Exception|Format-List -force
    }
}```

Logs- 
2020-09-17T19:21:59.3602523Z *** The column [dbo].[FileJob].[GMTOffset] on table [dbo].[FileJob] must be added, but the column has no default value and does not allow NULL values. If the table contains data, the ALTER script will not work. To avoid this issue you must either: add a default value to the column, mark it as allowing NULL values, or enable the generation of smart-defaults as a deployment option.
2020-09-17T19:21:59.4253722Z Updating database (Start)
2020-09-17T19:21:59.4274293Z An error occurred while the batch was being executed.
2020-09-17T19:21:59.4280337Z Updating database (Failed) 
2020-09-17T19:21:59.4330894Z ##[error]*** Could not deploy package. 
2020-09-17T19:22:00.3399607Z ##[error]PowerShell exited with code '1'. 
2020-09-17T19:22:00.7303341Z ##[section]Finishing: Update All Companies
4

0 回答 0