1
我想通过一个目录(按最小文件排序)循环,获取路径和文件名,然后将这些结果泵入一个utility.exe程序。PoshRSJob通过文件目录循环
我试图做到这一点的多线程与PoshRSJob,但我不甚至看到了应用程序在任务管理器中显示出来,我得到一个错误“null键没有在字面上的哈希不允许的。”对于每个存在的文件(如果50个文件在目录中,那么我得到50个错误)。我也无法测试节流是否奏效,因为没有任何实际运行。
Import-Module C:\PoshRSJob.psm1
Function MultiThread($SourcePath,$DestinationPath,$CommandArg, $MaxThreads){
if($CommandArg -eq "import") {
$fileExt = "txt"
}else{
$fileExt = "ini"
}
$ScriptBlock = {
Param($outfile, $cmdType, $fileExtension)
[pscustomobject] @{
#get the full path
$filepath = $_.fullname
#get file name (minus extension)
$filename = $_.basename
#build output directory
$destinationFile = "$($outfile)\$($filename).$($fileExtension)"
#command to run
$null = .\utility.exe $cmdType -source `"$filepath`" -target `"$destinationFile`"
}
}
#get the object of the passed source directory, and pipe it into start-rsjob
Get-ChildItem $SourcePath | Sort-Object length | Start-RSJob -ScriptBlock $ScriptBlock -ArgumentList $DestinationPath, $CommandArg, $fileExt -Throttle $MaxThreads
Wait-RSJob -ShowProgress | Receive-RSJob
Get-RSJob | Receive-RSJob
}
MultiThread "D:\input" "D:\output" "import" 3
哇,我发誓,我试过了。它的工作原理,谢谢 –
不客气。查看更新后的答案。更新日志:修正了(我认为)'Wait-RSJob'。 :-) –
我也注意到了,并在我看到您的更新评论之前修复了它。再次感谢 –