sqlpackage.exe 发布失败,没有错误

sqlpackage.exe publish fails with no error

我有下面的 powershell 脚本,它从 jenkins 针对 windows server 2019 奴隶运行:

$sqlpackagepublish = Start-Process -FilePath sqlpackage.exe -ArgumentList '/Action:Publish','/SourceFile:"Database Services\bin\Release\Database Services.dacpac"',"/TargetConnectionString:""Data Source=${Env};Integrated Security=True;Persist Security Info=False;Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False;Initial catalog=${Target}""","/p:BlockOnPossibleDataLoss=${Data_loss}" -wait -PassThru -Credential $Cred -RedirectStandardOutput sqlstdout.txt -RedirectStandardError sqlstderr.txt
$sqlpackagepublish.WaitForExit()
$sqlpackagepublish

if ($sqlpackagepublish.ExitCode -eq 0) {
     Get-Content sqlstdout.txt
 }
else {
     echo "An error occurred"
     Get-Content sqlstderr.txt
     exit $sqlpackagepublish.ExitCode
}

但是部署失败,sqlstderr.txt 中没有错误,jenkins 日志中也没有信息。知道如何调试它吗?

更新

根据下面的建议答案,我尝试了两种方法:

1.

删除 -PassThru 参数并读取文件内容。 所以我更改了以下代码:

$sqlpackagepublish = Start-Process -FilePath sqlpackage.exe -ArgumentList '/Action:Publish','/SourceFile:"Database Services\bin\Release\Database Services.dacpac"',"/TargetConnectionString:""Data Source=${Env};Integrated Security=True;Persist Security Info=False;Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False;Initial catalog=${Target}""","/p:BlockOnPossibleDataLoss=${Data_loss}" -wait -Credential $Cred
$sqlpackagepublish.WaitForExit()
$sqlpackagepublish

但现在我得到:

You cannot call a method on a null-valued expression.
+ $sqlpackagepublish.WaitForExit()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

稳健的 .NET 方式

在我原来的问题中,我有这些行:

$Username = $args[0]
$Password = $args[1]
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
$sqlpackagepublish = Start-Process -FilePath sqlpackage.exe -ArgumentList {args} -wait -PassThru -Credential $Cred

我不明白如何将它添加到您的代码中

这就是 Start-Process 命令的基本创建方式。 -PassThru 开关将输出重定向到一个对象(在本例中为 $sqlpackagepublish)。 更多关于 Start-Process 的信息:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-5.1

解决方案很少。

  1. 删除 -PassThru 参数并读取文件内容,就像您现在所做的那样
  2. 更难,但更健壮的 .NET 方式:
function Start-ProcessExecution
{
    param (
        [string] $exe,
        [string] $arguments,
        [string] $user,
        [string] $password
    )
    $startInfo = New-Object System.Diagnostics.ProcessStartInfo;

    $pass = ConvertTo-SecureString -AsPlainText $password -Force
    $startInfo.UserName = "$user";
    $startInfo.Password = "$pass";

    $startInfo.FileName = $exe;
    $startInfo.Arguments = $arguments;
    $startInfo.UseShellExecute = $false;
    $startInfo.RedirectStandardOutput = $true;
    $startInfo.RedirectStandardError = $true;

    $process = New-Object System.Diagnostics.Process;
    $process.StartInfo = $startInfo;

    $process.Start() | Out-Null;

    $output = $process.StandardOutput.ReadToEnd();
    $err = $process.StandardError.ReadToEnd();

    $process.WaitForExit();

    $obj = [PSCustomObject]@{
        ExitCode = $process.ExitCode
        StdOut = $output
        StdErr = $err
    }
    return $obj;
}

$exe = "sqlpackage.exe"
$arguments = [string]::Join(" ", "/Action:Publish", `
    '/SourceFile:"Database Services\bin\Release\Database Services.dacpac"', `
    '/TargetConnectionString:"Data Source=${Env};Integrated Security=True;Persist Security Info=False;Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False;Initial catalog=${Target}"', `
    '/p:BlockOnPossibleDataLoss=${Data_loss}')

$result = (Start-ProcessExecution -exe $exe -arguments $arguments -user $args[0] -password $args[1])
$ec = $result.ExitCode;

if ($ec -eq 0) {
    Write-Host "STDOUT:`n$($result.StdOut)";
}
else {
    Write-Host "STDERR:`n$($result.StdErr)";
}

Wrapper 基本上是为了提高可读性,其构造是为了防止在调用 WaitForExit() 后读取大量输出时出现死锁。