windows - Powershell 几个小时后崩溃

标签 windows powershell crash powershell-4.0

我的 Powershell 脚本遇到了奇怪的情况。 我编写了一个执行 .exe 文件的脚本 这个exe运行时间约为3小时,但在2小时后不断崩溃(或多或少1-2分钟)

我绞尽脑汁试图找出进程崩溃的原因 最终我发现.exe崩溃是因为powershell崩溃了。

下面是流程执行命令:

$Proc = Start-Process -FilePath $ExePath -ArgumentList $Arguments -NoNewWindow -PassThru
$Proc | Wait-Process -Timeout 28800 -ea 0 -ev timeouted

在我意识到这个问题是由 Powershell 引起的之后,我启用了 Windows Powershell 日志记录并发现错误消息“管道已停止”

enter image description here

脚本需要在进程结束后执行更多操作并获取其退出代码,这就是我使用 -PassThru 标志的原因。 我尝试在不使用 PassThru 标志或 Process-Wait 命令的情况下运行它,结果保持不变(进程在 2 小时后崩溃,但没有日志显示消息“管道已停止”)

要点:

  1. .exe 文件因 try;catch 记录器而变质,但崩溃时未记录任何内容 - 这不是 .exe 文件中的运行时错误
  2. 当独立于命令行运行 .exe 时,大约 3 小时后成功完成
  3. Powershell 脚本以管理员权限运行
  4. 该 exe 不会因 CPU/内存/磁盘使用率过高而导致崩溃

一旦有更多更新,我会跟进。

感谢所有帮助者。 非常感谢您的帮助!

最佳答案

在我看来,Start-Process cmdlet 适合快速完成任务。但是,当尝试调试 exe 不运行的原因时,它还有很多不足之处。

要在您的情况下解决此问题,使用 .Net 对象来重定向和更改有关实例化的某些内容可能会很有用。我在下面放置了一个示例函数,我在调试 exe 运行时遇到问题时使用过该函数。

function Start-Exe
{
    param
    ( [string]$exePath, [string]$args1 )

    $returnVal = $false
    $pinfo = New-Object System.Diagnostics.ProcessStartInfo
    $pinfo.FileName = $exePath
    $pinfo.RedirectStandardError = $true
    $pinfo.RedirectStandardOutput = $true
    $pinfo.UseShellExecute = $false
    $pinfo.Arguments = $args1
    $p = New-Object System.Diagnostics.Process
    $p.StartInfo = $pinfo
    $p.Start() | Out-Null
    $p.WaitForExit()
    $stdout = $p.StandardOutput.ReadToEnd()
    $stderr = $p.StandardError.ReadToEnd()
    $exitCode = $p.ExitCode
    #OutputFile can be some log file, or just use write-host to pump to console
    #$stdout | Add-Content $global:OutputFile
    #$stderr | Add-Content $global:OutputFile

    return $exitCode
}

关于windows - Powershell 几个小时后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44094351/

相关文章:

windows - MFC中如何获取子菜单?

powershell - 为什么 powershell 会重新排列我的输出?

powershell - 使用Powershell重命名.msg文件

android - 集成视频库。离开视频聊天后应用程序将重新启动

php - 使用isset函数使PHP崩溃

javascript - 打开 Javascript 文件时 Visual Studio 2015 崩溃

windows - 是否可以使用 Electron js 跟踪 Windows 中的进程?

c - 如何在 Windows-10 的 C 控制台程序中获取鼠标输入?

windows - 在 Windows 10 64 位中注册 DLL 而不将其移动到 System32

powershell - 如何将参数移交给Powershell脚本?