0

I have a function in Powershell that downloads .jpg-files (with invoke-webrequest) from a certain webserver. When I call the function in the powershell cli, it downloads the requested files. But when I wrap it into a start-job command, it works only if I add "receive-job". (And I want to use it with start-job, because it is part of a gui for my collegues and therefore needs a processbar).

With another function the "start-job" command works as expected. What am I doing wrong??

My code:

function DNBDownload ( $pfad, $ISBN ){
        $url = "https://a.certain.url/cover?isbn="
        $out_file = $( $pfad + "\" ) 
        $file_endung =".jpg"
            foreach ($i in $ISBN) {
                Invoke-WebRequest $url+$i -OutFile $( "$out_file$i$file_endung" )
}
}

$job1 = Start-Job -scriptblock ${function:DNBDownload} -argumentlist $path, $ISBN

$path comes from a folderBrowserDialog-call, $ISBN from the clipboard.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Chris
  • 23
  • 4
  • 1
    [`Start-Job`](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/start-job) ***doesn't*** starts only when I enter `Receive-Job`. It start right away simultaneously at the background. With [`Receive-Job`](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/receive-job), you receive the status/results of the concerned job. – iRon Sep 03 '21 at 10:43
  • @iRon: I know that start-job runs jobs in the background but in this case 1. the destination folder stays empty until I enter "receive-job", 2. the powershell cli only then shows a banner (in German) "Schreibe Webanforderung" and displays a counter for the bytes written. – Chris Sep 03 '21 at 11:00

2 Answers2

2

I still owe you my solution. It was quite simple: I had to suppress the progress bar (which windows displays as a banner) with $ProgressPreference = 'SilentlyContinue'. After adding this before the Invoke-WebRequest command my script runs as desired. I found my solution over there: Hide progress of Invoke-WebRequest .

Chris
  • 23
  • 4
0

It works for me (even doing a second one), but it doesn't show the progress bar until receive-job. The default folder is ~\documents. My test:

start-job { invoke-webrequest https://live.sysinternals.com/procmon.exe -outfile procmon.exe }
dir ~\documents\procmon.exe
job | receive-job -AutoRemoveJob -wait
js2010
  • 23,033
  • 6
  • 64
  • 66