How to Delay Complete-BitsTransfer until Transfer Complete?

1.3k views Asked by At

The following bits transfer starts a bunch of bits jobs, which work perfectly when I manually complete the transfer later: Get-BitsTransfer | Complete-BitsTransfer

My attempt to script a delay before complete is not working. The While is not processed, or perhaps it's processed before the transfer jobs even start.

loop starts many jobs...
    $job = Start-BitsTransfer -Source $fileUrl -Destination $fileDest -Asynchronous
...end loop

While ($job.JobState -eq "Transferring") {
    Sleep -Seconds 1
}

Get-BitsTransfer | Complete-BitsTransfer

Any suggestion how to permit -Asynchronous jobs a chance to load before executing complete?

After I added a 1-second delay before the wait, the script above waits. Seems hacky.

1

There are 1 answers

0
STWilson On

cezarypiatek deserves this answer:

Problem is the While loop checking only the last package. Code corrected to:

$job - @()
loop starts many jobs...
    $job += Start-BitsTransfer -Source $fileUrl -Destination $fileDest -Asynchronous
...end loop

While ($job | Where-Object{$job.JobState -eq "Transferring"}) {
    Sleep -Seconds 1
}

Get-BitsTransfer | Complete-BitsTransfer