function F2([String]$var2)
{
.........
.........
}
function F1([String]$var1)
{
.........
F2 $var2
.........
}
..................
..................
while ($i -le $count)
{
F1 "dir$i"
Start-Job -ScriptBlock ${function:F1} -ArgumentList @($i)
$i = $i + 1
}
在下面的代码片段中,我想启动一项作业并为 while 循环的每次迭代运行 F1。尽管 F1 中定义的内容工作正常,但当通过 ScriptBlock 完成时,F1 对 F2 的调用似乎无法完成。
我在这里缺少什么吗?
UPDATE 1
我尝试实现您的建议,以实现相当简单和直接的目标,即使用作业同时在目录中创建文件。
$dirname = "E:\TEST3\dir_"
$filename = "file_"
$i=1
$moduledef = {
function makeFiles([String]$dirname)
{
for ($i -le 5000)
{
echo "WASSSUP !!" >> "$dirnaname\$filename$i.txt"
$i++
}
}
function makeDir([String]$dirname)
{
mkdir "$dirname$i"
makeFiles "$dirname$i"
$i++
}
} # END OF $moduledef
New-Module -Name MyFunctions -ScriptBlock $moduledef
while($i -le 10)
{
Start-Job -ScriptBlock {
param([String]$jobArg)
New-Module -Name MyFunctions -ScriptBlock $Using:moduledef
makeDir $jobArg
} -ArgumentList @("$dirname$i")
$i++
}
Get-Job | Wait-Job
这里的问题是作业失败了(我已将其中一个粘贴在下面),知道我在这里做错了什么吗?
HasMoreData : True
StatusMessage :
Location : localhost
Command :
param([String]$jobArg)
New-Module -Name MyFunctions -ScriptBlock $Using:moduledef
makeDir $jobArg
JobStateInfo : Failed
Finished : System.Threading.ManualResetEvent
InstanceId : e79a6489-cad3-47a6-b6f4-8a207d32c187
Id : 79
Name : Job79
ChildJobs : {Job80}
PSBeginTime : 4/14/2018 9:52:01 PM
PSEndTime : 4/14/2018 9:52:02 PM
PSJobTypeName : BackgroundJob
Output : {}
Error : {}
Progress : {}
Verbose : {}
Debug : {}
Warning : {}
Information : {}
State : Failed
UPDATE 2
我包含了一个接收作业,这只是错误的一部分。我正在尝试解决这个问题,但将其粘贴在这里希望您能帮助我。
PSPath : Microsoft.PowerShell.Core\FileSystem::E:\TEST3\dir10
PSParentPath : Microsoft.PowerShell.Core\FileSystem::E:\TEST3
PSChildName : dir10
PSDrive : E
PSProvider : Microsoft.PowerShell.Core\FileSystem
PSIsContainer : True
Mode : d-----
BaseName : dir10
Target : {}
LinkType :
RunspaceId : 434aa380-b888-4ca5-897e-75a88e1f6560
Name : dir10
FullName : E:\TEST3\dir10
Parent : TEST3
Exists : True
Root : E:\
Extension :
CreationTime : 4/14/2018 9:29:12 PM
CreationTimeUtc : 4/14/2018 3:59:12 PM
LastAccessTime : 4/14/2018 9:29:12 PM
LastAccessTimeUtc : 4/14/2018 3:59:12 PM
LastWriteTime : 4/14/2018 9:29:12 PM
LastWriteTimeUtc : 4/14/2018 3:59:12 PM
Attributes : Directory
The term 'makeFiles' 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.
+ CategoryInfo : ObjectNotFound: (makeFiles:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : localhost
Cannot bind parameter 'ScriptBlock'. Cannot convert the "
function makeFiles([String]$dirname)
{
for ($i -le 5000)
{
echo "WASSSUP !!" >> "$dirnaname\$filename$i.txt"
$i++
}
}
function makeDir([String]$dirname)