我有以下代碼。Powershell start-job -scriptblock無法識別在同一個文件中定義的函數?
function createZip
{
Param ([String]$source, [String]$zipfile)
Process { echo "zip: $source`n --> $zipfile" }
}
try {
Start-Job -ScriptBlock { createZip "abd" "acd" }
}
catch {
$_ | fl * -force
}
Get-Job | Wait-Job
Get-Job | receive-job
Get-Job | Remove-Job
但是,該腳本返回以下錯誤。
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
309 Job309 Running True localhost createZip "a...
309 Job309 Failed False localhost createZip "a...
Receive-Job : The term 'createZip' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:17 char:22
+ Get-Job | receive-job <<<<
+ CategoryInfo : ObjectNotFound: (function:createZip:String) [Receive-Job], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
似乎在腳本塊start-job
內部無法識別函數名稱。我也試過function:createZip
。
謝謝。看來在PowerShell的另一個實例中引發的異常無法捕獲。捕獲異常的最佳方法是什麼? – ca9163d9 2012-01-05 22:48:46
@NickW你當然可以。看看我更新的答案。 – 2012-01-05 23:02:34
謝謝你 - 我有一個類似的問題,現在我是金! – marceljg 2012-04-18 15:20:52