【问题标题】:Track windows process exit codes跟踪 Windows 进程退出代码
【发布时间】:2019-01-10 13:08:05
【问题描述】:

在我们的某些 Citrix Desktop 上,我们遇到了随机终止(崩溃?)应用程序的问题。事件日志没有显示任何内容。

我的想法是跟踪进程的启动和停止以及它们的退出/错误代码一段时间,并尝试找到一个模式。

我的方法是通过 Powershell:

function Enable-ProcessStopTrace {            
[CmdLetBinding()]            
param(            
)             
$Query = "Select * From __InstanceDeletionEvent within 2 Where TargetInstance ISA 'Win32_Process'"            
$Identifier = "StopProcess"            
$ActionBlock = {            
 $e = $event.SourceEventArgs.NewEvent.TargetInstance            
 write-host ("Process {0} with PID {1} has stopped at {2}" -f $e.Name, $e.ProcessID, $event.TimeGenerated)      


 $fmt = 'ProcessStopped: (ID={0}, Parent={1}, Time={2}, Name="{3}", ExitCode={4})'            
 $msg = $fmt -f $e.ProcessId, $e.ParentProcessId, $event.TimeGenerated, $e.Name, $e.Exitcode

 write-host ($msg)            
}            
Register-WMIEvent -Query $Query -SourceIdentifier $Identifier -Action $ActionBlock            
}

Enable-ProcessStopTrace

(无耻地从博文中复制并修改)

每次进程终止时我都会得到一个不错的输出,但“exitcode”总是空的。

这是正确的方法吗?我可以通过这种方式获取退出代码或错误代码吗? 还是我必须走完全不同的路线?

【问题讨论】:

    标签: powershell process wmi exit-code


    【解决方案1】:

    我可以让它工作的唯一方法是:

    https://social.technet.microsoft.com/Forums/lync/en-US/5b318b42-27c9-49d1-8c19-c5d7f0b9dbb2/getting-exit-code-from-startprocess?forum=winserverpowershell

    PS C:\> $notepad = Start-Process notepad++ -Wait -PassThru
    <close the application at this point>
    PS C:\> $notepad.exitcode
    PS C:\> 0
    

    脚本在后台运行(等待)直到应用程序关闭(无论是否正常),然后您可以运行 $applicationname.exitxcode 来获取退出代码。

    【讨论】:

    • 嗯,看起来只有当我通过 PS 显式启动应用程序时才有效。我正在寻找一种跟踪所有应用程序的解决方案。尤其是那些自动启动的,比如“explorer”。但这是一个开始。我会测试它。也许它揭示了一些东西。
    猜你喜欢
    • 1970-01-01
    • 2018-12-16
    • 2022-01-22
    • 2019-12-19
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    相关资源
    最近更新 更多