2017-02-26 77 views
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 

回答

2

您的scriptblock正在创建一个对象,您将$null = .\utility.exe +++定义为属性。正如它所说的,$null(无)的值不能是一个属性名称..我建议只是运行线..

您也许还想更改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 
+0

哇,我发誓,我试过了。它的工作原理,谢谢 –

+0

不客气。查看更新后的答案。更新日志:修正了(我认为)'Wait-RSJob'。 :-) –

+0

我也注意到了,并在我看到您的更新评论之前修复了它。再次感谢 –