PoshRSJob循环遍历文件目录

时间:2017-02-26 13:55:48

标签: multithreading powershell

我正在尝试遍历目录(按最小文件排序),获取路径和文件名,然后将这些结果泵入utility.exe程序。

我正在尝试使用PoshRSJob执行此多线程,但我甚至没有看到实用程序显示在任务管理器中,我收到错误"哈希文字中不允许使用空键。 #34;,对于每个存在的文件(如果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

1 个答案:

答案 0 :(得分:2)

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

您可能还想更改Wait-RSJob - 部分。你没有指定一份工作,所以它永远不会等待任何事情。尝试:

尝试将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" 3