将静默 SQL Server 安装的输出重定向到 C# 中的标准错误

本文关键字:错误 标准 重定向 输出 静默 SQL Server 安装 | 更新日期: 2023-09-27 18:33:27

我有以下代码:

Process p = new Process();
p.StartInfo.FileName = "SQLEXPRADV_x86_ENU.exe";
p.StartInfo.Arguments = "/CONFIGURATIONFILE=Config.ini";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
string line;
while ((line = p.StandardOutput.ReadLine()) != null)
{
   tOutputStandard.Text += line + "'r'n";
}
while ((line = p.StandardError.ReadLine()) != null)
{
   tOutputError.Text += line + "'r'n";
}
p.WaitForExit();

配置文件设置为静默运行。tOutputStandard 和 tOutputError 是将显示输出的文本框。

但是,它不显示 SQL Server 安装的任何输出。 我尝试运行其他应用程序,并且它们正确发送输出,因此我认为代码没有问题。

如何让 SQL Server 通过管道将其输出到 C#?我需要知道如果安装失败会是什么错误。

将静默 SQL Server 安装的输出重定向到 C# 中的标准错误

也许这会有所帮助。删除以下代码片段:

string line;
while ((line = p.StandardOutput.ReadLine()) != null)
{
   tOutputStandard.Text += line + "'r'n";
}
while ((line = p.StandardError.ReadLine()) != null)
{
   tOutputError.Text += line + "'r'n";
}

改为连接到事件处理程序 OutputDataReceived 和 ErrorDataReceived:

p.OutputDataReceived += this.OutputDataReceived;
p.ErrorDataReceived += this.ErrorDataReceived;

回调方法如下所示。

private void ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    tOutputError.Text += e.Data + "'r'n";
}
private void OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    tOutputStandard.Text += e.Data + "'r'n";
}

因此,事实证明,此问题的真正原因是,如果直接从MSI运行,SQL安装将不会输出任何内容。

相反,您必须首先运行 MSI,并将其解压缩到临时目录,然后在该目录中运行安装程序 exe。 此 exe 将为您提供所需的输出。

Markus Safar 使用事件处理程序发布的答案也可能有助于满足其他需求。

如果无法

使管道正常工作,则始终可以只读取安装日志文件。

我也通过Powershell使用静默安装。对于错误,我查看最新文件夹中的安装程序引导日志文件夹,并遍历 Details.txt 文件中包含"错误"的行

    C:'Program Files'Microsoft SQL Server'110'Setup Bootstrap'Log'<datestamp>'Details.txt 

不完美,但总比没有输出好。