2

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)

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I am a beginner and that helped a lot
0

Use the .waitforexit method.

Example:

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

After the $proc.WaitForExit() line you can open your file.

Its look much better than a loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.