简体   繁体   English

Powershell:如何在永无止境的脚本行之后继续运行脚本?

[英]Powershell: How do I continue running script after never-ending script line?

I have a line in my script that downloads a large video file.我的脚本中有一行下载大型视频文件。 After the download starts I want to already open the file while is it downloading.下载开始后,我想在下载文件时已经打开文件。 The problem is, is that the download command hasn't finished yet so the script stays stuck on the same line.问题是,下载命令还没有完成,所以脚本停留在同一行。

(Download-File command)

$allFiles = Get-ChildItem | Select-Object -Property name,LastAccessTime | measure-object -Property LastAccessTime -Maximum
$videoFile = Get-ChildItem | Where-Object { $_.LastAccessTime -eq $allFiles.Maximum}
Start-Process $videoFile

(I want this to run in a loop while the download-file command is running) (我希望它在下载文件命令运行时循环运行)

That should be easy.那应该很容易。 All you need to do is make it run on a different thread.您需要做的就是让它在不同的线程上运行。 Use background jobs or Runspaces.使用后台作业或运行空间。 Below example is Background Job.下面的例子是后台作业。

$ScriptBlock = {(Download-File command)}
Start-Job -ScriptBlock $ScriptBlock

do
{
    $allFiles = Get-ChildItem | Select-Object -Property name,LastAccessTime | measure-object -Property LastAccessTime -Maximum
    $videoFile = Get-ChildItem | Where-Object { $_.LastAccessTime -eq $allFiles.Maximum}
    Start-Process $videoFile
}
while (1 -gt 0)

Although, I am not sure if you would want to open the video file in a loop.虽然,我不确定您是否想循环打开视频文件。 If it does support opening an incomplete video file, you will have just as many instances of it.如果它确实支持打开不完整的视频文件,您将拥有同样多的实例。 Better enclose it in an if (!(Get-Process -name $VideoFile)){} loop to prevent that.最好将其包含在if (!(Get-Process -name $VideoFile)){}循环中以防止出现这种情况。

Use the .waitforexit method.使用 .waitforeexit 方法。

Example:例子:

$proc = Start-Process cmd.exe -PassThru
$proc.WaitForExit()

After the $proc.WaitForExit() line you can open your file.$proc.WaitForExit()行之后,您可以打开您的文件。

Its look much better than a loop它看起来比循环好得多

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

相关问题 在一个复制命令停止后,如何获取Shell脚本以继续复制文件? - How do I get a shell script to continue copying files after one copy command stalls? Shell脚本完成运行后,继续Maya Python脚本 - Continue Maya Python Script After Shell Script Finished Running 如何从Java运行Shell脚本并使其在JVM关闭后继续运行? - How to run a shell script from Java and have it continue running after JVM shutdown? 如何在运行Shell脚本后使Mac“ .command”文件自动退出? - How do I get a Mac “.command” file to automatically quit after running a shell script? 失败后shell脚本继续 - Shell script continue after failure 如何在shell脚本中启动程序并使shell脚本继续,即使程序保持打开状态 - How do I launch a program inside a shell script and have the shell script continue, even though the program remains open Shell脚本:如何读取带超时的输入,但在按Enter键时不能继续? - Shell Script: How do I read input with a timeout, but not continue when enter is pressed? 尊重条件后如何继续处理shell脚本 - How continue processing shell script after respect of condition 结束bash脚本后保持目录更改 - Stay in directory changed after ending of bash script 以任何方式在其他Shell中继续运行脚本 - Any way to continue the running script in other shell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM