2017-02-26 111 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

我也注意到了,並在我看到您的更新評論之前修復了它。再次感謝 –