我尝试遍历一个目录(按最小文件排序),获取路径和文件名,然后将这些结果放入utility.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发布于 2017-02-26 22:01:12
您的scriptblock正在创建一个对象,您在其中将$null = .\utility.exe +++定义为一个属性。正如它所说的,$null (nothing)的值不能是属性名。我建议你直接运行这些线路..
您可能还希望更改Wait-RSJob-part。您不指定作业,因此它永远不会等待任何东西。尝试:
尝试将scriptblock更改为:
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" 3https://stackoverflow.com/questions/42469370
复制相似问题