2

I am using Start-ThreadJob and ScriptBlock to execute a powershell script in a new thread. It works fine on my local but on the preprod server, I am getting an error.

Code Block where I am initiating a new thread

Start-ThreadJob -InputObject $fileType -ScriptBlock {
          ./Functions/Download-FilesFromFTP.ps1 $args[0] $args[1]  $args[2] $args[3] $args[4] $args[5]
        } -ArgumentList $ftpServer,$user,$password,$completeSourceFolder,$completeStagingFolderPath,$completeLogFolderPath

As mentioned earlier, this code block works perfectly on my local. On Preprod env I get the following error when I display jobs using Get-Jobs command. enter image description here

Powershell version on my local enter image description here

Powershell version on preprod server enter image description here

The version of the module ThreadJob is same on both servers

Tarun Bhatt
  • 727
  • 2
  • 8
  • 28

1 Answers1

2

Start-ThreadJob runs the new thread with the same current location as the caller, which is unrelated to where the executing script is located.

If you want to refer to a file relative to the script's own location, use the automatic $PSScriptRoot variable, and refer to it in the thread script block via the $using: scope:

Start-ThreadJob -InputObject $fileType -ScriptBlock {
  & "$using:PSScriptRoot/Functions/Download-FilesFromFTP.ps1" @args
} -ArgumentList $ftpServer,$user,$password,$completeSourceFolder,$completeStagingFolderPath,$completeLogFolderPath

Note the use of @args in order to also pass all positional arguments, reflected in the automatic $args array, through as individual arguments to the target script via splatting.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • If this was the problem, how is this script working on my local? Thanks for your response though, I will try to implement what u r saying – Tarun Bhatt Mar 26 '22 at 02:04
  • 1
    @TarunBhatt, it would only work on your local machine _if the current location (`$pwd`) happens to be the same location in which your script is located_ - assuming that `./Functions/Download-FilesFromFTP.ps1` is relative to where your script is located. – mklement0 Mar 26 '22 at 02:06
  • 1
    I think I get what you are saying. When the script was working on my local, its running from a terminal within VSCode but when I run the same script on my local from a separate Powershell command prompt, it gives the same error. I will go through ur solution again and implement ur advice – Tarun Bhatt Mar 26 '22 at 02:13
  • Although I am now able to simulate the issue on my local by using an independent powershell console instead of VSCode terminal, I still don't why is this an issue. The pwd command from both locations return the same location – Tarun Bhatt Mar 26 '22 at 02:26
  • @TarunBhatt, you don't need to solve this mystery if you know that the relative path `./Functions/Download-FilesFromFTP.ps1` should be relative _to the enclosing script file's location_. If that is indeed the intent, the only reliable way to invoke this path is via `$PSScriptRoot`. – mklement0 Mar 26 '22 at 02:28
  • This worked. Although not the ideal solution. Your original comment on this post would be the way to go and I will learn and implement it that way – Tarun Bhatt Mar 26 '22 at 04:16