我试图遍历一个目录(按最小文件排序),获取路径和文件名,然后将这些结果泵入一个实用程序.exe 程序。
我正在尝试使用 PoshRSJob 进行多线程处理,但我什至没有看到实用程序出现在任务管理器中,我收到错误“哈希文字中不允许使用空键。”,对于每个存在的文件(如果目录中有 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
您的脚本块正在创建一个您定义$null = .\utility.exe +++
为属性的对象。正如它所说,$null
(nothing) 的值不能是属性名称.. 我建议只运行这些行..
您可能还想更改Wait-RSJob
-part。您没有指定工作,因此它永远不会等待任何事情。尝试:
尝试将脚本块更改为:
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)
#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
Get-RSJob | Wait-RSJob -ShowProgress | Receive-RSJob
}
MultiThread "D:\input" "D:\output" "import" 3
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句