我正在使用 Start-ThreadJob 和 ScriptBlock 在新執行緒中執行 powershell 腳本。它在我的本地作業正常,但在 preprod 服務器上,我收到錯誤訊息。
我正在啟動新執行緒的代碼塊
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 環境中,當我使用 Get-Jobs 命令顯示作業時出現以下錯誤。

我本地的 Powershell 版本

preprod 服務器上的 Powershell 版本

兩臺服務器上模塊 ThreadJob 的版本相同
uj5u.com熱心網友回復:
Start-ThreadJob以與呼叫者相同的當前位置運行新執行緒,這與執行腳本所在的位置無關。
如果要參考相對于腳本自身位置的檔案,請使用自動$PSScriptRoot變數,并通過$using:范圍在執行緒腳本塊中參考它:
Start-ThreadJob -InputObject $fileType -ScriptBlock {
& "$using:PSScriptRoot/Functions/Download-FilesFromFTP.ps1" @args
} -ArgumentList $ftpServer,$user,$password,$completeSourceFolder,$completeStagingFolderPath,$completeLogFolderPath
注意使用order 也可以通過splatting@args將反映在自動$args陣列中的所有位置引數作為單獨的引數傳遞給目標腳本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/450260.html
標籤:多线程 电源外壳 powershell 模块
下一篇:有條件的RAII鎖定
