【发布时间】:2016-08-27 18:45:30
【问题描述】:
我在单独的文件中有函数,我需要在一个主文件中作为作业运行。
我需要能够传递这些函数参数。
这就是我所拥有的:
testJobsMain.ps1:
$Functions = {
. c:\.ps\func1.ps1
. c:\.ps\func2.ps1
}
$arrOutput = @()
foreach($i in ('aaa','bbb','ccc','ddd','eee') ) {
$ExecutionBlock = {
FOO -myArg $i
FOO2 -blah 'zzzzzzz'
}
$arrOutput += Start-Job -InitializationScript $Functions `
-ScriptBlock $ExecutionBlock |
Wait-Job | Receive-Job
}
$arrOutput
func1.ps1
function FOO( $myArg ) { write-output $myArg }
func2.ps1
function FOO2( $blah ) { write-output $blah }
这是 testJobsMain.ps1 的输出:
PS> .\testjobsMain.ps1
zzzzzzz
zzzzzzz
zzzzzzz
zzzzzzz
$i 没有传递给函数。我该怎么做?
【问题讨论】:
-
尝试用
FOO -myArg $using:i替换FOO -myArg $i和/或使用GetNewClosure()
标签: powershell closures powershell-2.0 powershell-3.0 jobs