简体   繁体   English

PowerShell:写进度不能与RoboCopy一起使用,解决方法?

[英]PowerShell: Write-Progress not working with RoboCopy, Workaround?

I have successfully incorporated Write-Progress into my scripts. 我已成功将Write-Progress合并到脚本中。 When my scripts get to any portion where RoboCopy is used Write-Progress disappears and reappears when RoboCopy is complete: 当我的脚本到达使用RoboCopy任何部分时, RoboCopy完成后, Write-Progress消失并重新出现:

Write-Progress -Id 1 -Activity $arg2 -PercentComplete $i -Status $arg1
RoboCopy C:\source C:\destination /MIR

I have considered using Start-Process to run Write-Progress but have had limited success. 我曾考虑过使用Start-Process来运行Write-Progress但是成功有限。 Any help would be gladly appreciated. 任何帮助将不胜感激。

I was able to answer my own question. 我能够回答自己的问题。 Here is the solution by using Start-Job : 这是使用Start-Job的解决方案:

Write-Progress -Id 1 -Activity $arg2 -PercentComplete $i -Status $arg1
Start-Job { RoboCopy C:\source C:\destination /MIR LOG+:C:\log.txt } > $null
cat C:\log.txt

So let me explain. 所以让我解释一下。 Start-Job creates a background job and redirects it's own output to the bit-bucket. Start-Job创建一个后台作业,并将其自身的输出重定向到位桶。 Since it's a background job, I'm immediately returned to run more stuff, like cat which spills all the contents of RoboCopy's log file. 由于这是一项后台工作,因此我立即返回运行更多的东西,例如cat,它泄漏了RoboCopy日志文件的所有内容。 In the end, it works as it should to begin with. 最后,它起初应该起作用。 YAY! 好极了!

I suspect Robocopy takes over the console display buffer to display progress. 我怀疑Robocopy会接管控制台显示缓冲区以显示进度。 Try using the robocopy options /NP /NJH /NJS . 尝试使用robocopy选项/NP /NJH /NJS You may also need to add /NS /NC /NFL /NDL . 您可能还需要添加/NS /NC /NFL /NDL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM