Search code examples
functionpowershellstart-job

I don't understand why "start-job" starts only when I enter "receive-job"


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.


Solution

  • 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 .