gpt4 book ai didi

windows - Powershell 几个小时后崩溃

转载 作者:行者123 更新时间:2023-12-03 00:40:54 27 4
gpt4 key购买 nike

我的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com