[英]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.