Start-ThreadJob ScriptBlock 问题无法找到 powershell 脚本

Issue with Start-ThreadJob ScriptBlock Unable to find powershell script

我正在使用 Start-ThreadJob 和 ScriptBlock 在新线程中执行 powershell 脚本。它在我的本地工作正常,但在预生产服务器上,我收到一个错误。

我启动新线程的代码块

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

如前所述,此代码块在我的本地完美运行。在 Preprod env 上,当我使用 Get-Jobs 命令显示作业时出现以下错误。

我本地的 Powershell 版本

预生产服务器上的 Powershell 版本

两个服务器上的模块 ThreadJob 的版本相同

Start-ThreadJob 在与调用者相同的当前位置运行新线程,这与执行脚本所在的位置无关。

如果要引用相对于 脚本自身位置 的文件,请使用 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

注意 @args 的使用也是为了传递所有位置参数,反映在 automatic $args array, through as individual arguments to the target script via splatting.