我在单独的文件中有函数,我需要在一个主文件中作为作业运行。
我需要能够传递这些函数参数。
这就是我所拥有的:
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
没有传递给函数。我怎么能这样做?
答案 0 :(得分:1)
您必须将其作为参数传递给此
$ExecutionBlock = {
param($passedI)
FOO -myArg $passedI
FOO2 -blah 'zzzzzzz'
}
$arrOutput += Start-Job -InitializationScript $Functions `
-ScriptBlock $ExecutionBlock -ArgumentList $i |
Wait-Job | Receive-Job
或使用using
修饰符(信用转到Eris),如此
...
FOO -myArg $using:i
...