在Powershell中安装NServicebus服务时捕获错误
Capturing errors when installing NServicebus service in Powershell
所以,我有一个 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 脚本本身已成功完成。
我可以解析错误代码或一些 'exception' 文本的输出文本,但这似乎令人遗憾地不能令人满意。
我认为这不是 Powershell 问题。当我 运行 它只是通过命令行检查 %errorlevel%
我得到 0
。此外,其他几个执行相同操作的示例在线脚本也忽略了任何错误传播。
有什么建议吗?
我会 运行 检查错误日志
快速而肮脏...根据需要进行编辑。
$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'
}
为了详细说明我对 OP 的评论,当您使用 NServiceBus.Host.exe 安装 Windows 服务时,它最终会从 NServiceBus.Hosting.Windows.Installers
命名空间调用 WindowsInstaller.RunInstall
来执行安装:
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
),因此您唯一的选择是解析应用程序的标准输出。
所以,我有一个 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 脚本本身已成功完成。
我可以解析错误代码或一些 'exception' 文本的输出文本,但这似乎令人遗憾地不能令人满意。
我认为这不是 Powershell 问题。当我 运行 它只是通过命令行检查 %errorlevel%
我得到 0
。此外,其他几个执行相同操作的示例在线脚本也忽略了任何错误传播。
有什么建议吗?
我会 运行 检查错误日志
快速而肮脏...根据需要进行编辑。
$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'
}
为了详细说明我对 OP 的评论,当您使用 NServiceBus.Host.exe 安装 Windows 服务时,它最终会从 NServiceBus.Hosting.Windows.Installers
命名空间调用 WindowsInstaller.RunInstall
来执行安装:
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
),因此您唯一的选择是解析应用程序的标准输出。