powershell 在写入标准输出时保持进度条可见

sqougxex  于 2023-11-18  发布在  Shell
关注(0)|答案(1)|浏览(179)

我有一个PowerShell安装脚本,它会调用其他几个脚本,这些脚本会在stdout的过程中写入stdout。通过查看stdout,无法确定安装脚本已经沿着了多远。因此,我想显示一个进度条。
我尝试使用Write-Progress,但只要将某些行写入stdout,进度条就会向上移动并消失在视图中。
有没有一种方法可以让进度条粘在终端的顶部,而不会超出视图?写入stdout的内容可能会表现出价值,所以我不想为了保持进度条可见而丢弃输出。

zxlwwiss

zxlwwiss1#

今天我有一个类似的情况下,发现你的问题没有得到解决。我想出了这样的东西。希望它有帮助。基本上我开始一个后台作业与我的长期运行的任务和保存其id。我做Receive-Job每2秒得到它的输出,然后我重新打印我的Write-Progress

  1. Function Replace-GhorsePlots {
  2. Write-Progress -Id 1 -Activity "Replacing Ghorse Plots" -Status "Starting" -PercentComplete 0
  3. $PlotCount = Get-GhorsePlots | Measure-Object | Select-Object -ExpandProperty Count
  4. $i=0
  5. Get-GhorsePlots | ForEach-Object {
  6. $PlotFile = $_
  7. Write-Progress -Id 1 -Activity "Replacing Ghorse Plots" -Status ("{0} of {1} Plots replaced. Replacing {2}" -f $i, $PlotCount, $PlotFile.Name) -PercentComplete ($i / $PlotCount * 100)
  8. #Remove on Ghorse Plot and instead plot one Bladebit Plot
  9. $NewPlotDestPath=$PlotFile.DirectoryName.Replace("/ghorse", "/bladebit")
  10. # Write-Host("Replacing " + $PlotFile.FullName + " with a new plot in " + $NewPlotDestPath)
  11. Remove-Item -Path $PlotFile.FullName -Confirm:$false
  12. # Create PlotJob to reference to the ID later
  13. $PlotJob = Start-Job -ArgumentList @($NewPlotDestPath) -ScriptBlock {
  14. param(
  15. $NewPlotDestPath
  16. )
  17. # To be sure we have the Module on the new process
  18. Import-Module -Name ChiaShell -DisableNameChecking
  19. # This lasts really long and prints a lot on stdout
  20. Invoke-BladebitPlotter -DestDir $NewPlotDestPath -Count 1
  21. # Start-Sleep -Seconds 1
  22. }
  23. #Wait for PlotJob to finish and show progress
  24. do {
  25. Start-Sleep -Seconds 2
  26. $PlotJob = Get-Job -Id $PlotJob.Id
  27. Write-Progress -Id 1 -Activity "Replacing Ghorse Plots" -Status ("{0} of {1} Plots replaced. Replacing {2}" -f $i, $PlotCount, $PlotFile.Name) -PercentComplete ($i / $PlotCount * 100)
  28. Receive-Job -Job $PlotJob
  29. } while ($PlotJob.State -eq "Running")
  30. # Finished -> Remove
  31. Remove-Job -Id $PlotJob.Id
  32. $i++
  33. }
  34. Write-Progress -Id 1 -Activity "Replacing Ghorse Plots" -Status "Finished" -PercentComplete 100 -Completed
  35. }

字符串

展开查看全部

相关问题