所以,我有一个powershell脚本来安装NServiceBus服务作为Windows服务。
Invoke-Expression "$fullNsbHostPath $arguments"
为了完整起见,我尝试了Invoke-Expression
和Start-Process
:
Start-Process -Wait -NoNewWindow -FilePath $fullNsbHostPath -ArgumentList $arguments -RedirectStandardOutput $tempFileName -ErrorVariable $errvar
它启动安装程序就好了,在某些情况下报告例外:
Failed to execute installers: System.Data.SqlClient.SqlException
(0x80131904): A connection was successfully established with the
server, but then an error occurred during the login process.
(provider: Shared Memory Provider, error: 0 - No process is on the
other end of the pipe.) ---> System.ComponentModel.Win32Exception
(0x80004005): No process is on the other end of the pipe ... Error
....
Number:233,State:0,Class:20
我对此很满意。我期待例外。但是,该过程本身并未表明它已失败。实际上,Powershell脚本本身已成功完成。
我可以解析输出文本中的错误代码或者某些“异常”文本,但这似乎令人遗憾地不令人满意。
我认为这不是Powershell问题。当我仅通过命令行运行它并检查%errorlevel%
时,我得到0
。此外,其他几个相同的在线脚本也会省略任何错误传播。
有什么建议吗?
答案 0 :(得分:1)
我会检查错误日志
快速而肮脏......根据需要进行编辑。
$logfile = 'logfilelocation'
$Complete = 'no'
$Counter = 1
$max = 6
DO {
$Check = SELECT-STRING -pattern 'status: 0.' -path $logfile -quiet
write-host $Check
If (($Check) -eq $True) {
Set-Variable -name Complete -Value "yes"
}
Else {Set-Variable -name Complete -Value 'no'}
Write-host $Counter
Start-Sleep 20
$Counter++
}
while ($Complete -eq 'no')
If (($Counter) -eq $max) {
Throw 'Installation failed, check the error log'
}
答案 1 :(得分:1)
要详细说明我对OP的评论,当您使用NServiceBus.Host.exe安装Windows服务时,它最终会从WindowsInstaller.RunInstall
命名空间调用NServiceBus.Hosting.Windows.Installers
来执行安装:
private static void RunInstall()
{
Console.WriteLine("Executing the NServiceBus installers");
try
{
WindowsInstaller.host.Install();
}
catch (Exception ex)
{
Console.WriteLine("Failed to execute installers: " + ex);
}
}
如您所见,此方法捕获安装过程中的所有异常,并将它们写入控制台标准输出流。它不会写入错误输出流(Console.Error
)或设置退出代码(Environment.ExitCode
),因此您唯一的选择是解析应用程序的标准输出。