scriptblock argumentlist question RRS feed

  • Question

  • Hi all,

    I am trying to write a script that gets arguments from a couple of csv file.

    each csv contains 1 number on each line.


     $batchListFolder = "c:\temp\batch"
     $location = Get-Location  
     $parameter1 = "test1"
     $parameter2 = "test3"
     $scriptBlock = {
      param($path, $source, $target, $parameter1, $parameter2, $logfile)
      cd $path
      .\anotherscript.ps1 -Sourcex $source -Targetx $target -p1 parameter1 -p2 parameter1 -LogFile $logfile
     Get-ChildItem $batchListFolder -Filter *.txt | 
     Foreach-Object {
      Start-Job -ScriptBlock $scriptBlock -ArgumentList @($location.Path, "$batchListFolder\$($_.Name)" , "tmp"+"$batchListFolder\$($_.Name)" ,$parameter1 ,$parameter2, "$batchListFolder\%nr%.log")
      $activeJobs = @(Get-Job | Where-Object { $_.State -eq 'Running' })
      while ( $activeJobs.Count -gt 4)
       Write-Progress -Activity "Waiting for job to finish" -Status "($($activeJobs.Count) jobs currently running)"
       Start-Sleep -s 5
       $activeJobs = @(Get-Job | Where-Object { $_.State -eq 'Running' })
      Write-Progress -Activity "Waiting for job to finish" -Completed
     while ( $activeJobs.Count -gt 0)
      Write-Progress -Activity "All jobs started, waiting for them to complete" -Status "($($activeJobs.Count) jobs currently running)"
      Start-Sleep -s 5
      $activeJobs = @(Get-Job | Where-Object { $_.State -eq 'Running' })
     Write-Progress -Activity "All jobs started, waiting for them to complete" -Completed
     Write-Host "Complete!" - ForegroundColor Green

    So on the line:

     Start-Job -ScriptBlock $scriptBlock -ArgumentList @($location.Path, "$batchListFolder\$($_.Name)" , "tmp"+"$batchListFolder\$($_.Name)" ,$parameter1 ,$parameter2, "$batchListFolder\%nr%.log")

    the second argument is the number from the csv , the third needs to be "tmp"+nrfromcsv

    But this doesnt work.  When I run the code the second argument is 234 for example but third argument becomes "tmp+c:\temp\batch\1.txt" instead of tmp234

    Anyone can help me to understand what I am doing wrong?


    • Edited by bashr Friday, September 8, 2017 12:18 AM syntax error
    • Moved by Bill_Stewart Tuesday, November 7, 2017 9:29 PM Question not clear; abandoned
    Thursday, September 7, 2017 10:35 PM

All replies

  • First - do not post code with line numbers.  It is hard to read and cannot be easily copied for testing.

    I looked at this and it is full of logic, syntax and other technical errors.  I recommend spending some time testing your code a bit at a time.


    Thursday, September 7, 2017 10:58 PM
  • Hi thanks for replying.

    I removed the line numbers like you requested.

    I am not sure what you are refering to?

    If I hard code the correct arguments in the line with the problem the scripts is working.

    I'm just having troubles with the third parameter.


    Thursday, September 7, 2017 11:52 PM
  • Your parameters are missing a comma.


    Thursday, September 7, 2017 11:53 PM
  • ok, I missed that coma when I added/removed the line numbers...

    Comma is there now.

    Friday, September 8, 2017 12:20 AM
  • And?  What is your question now?


    Friday, September 8, 2017 12:22 AM
  • Well in this line I cannot get the third argument correct.

    the second is the line number from the csv and I would like third argument to be tmp+the second argument but I'm doing something wrong because when i check my log the third argument is now  "tmp+c:\temp\batch\1.txt" instead of tmp234 .

    I guess it's not correct to use "$batchListFolder\$($_.Name)" two times in 1 loop but I don't get how I can do this.

    So pointing me in the good direction would be much appreciated.

    Friday, September 8, 2017 12:36 AM
  • That line looks like this:

    	'tmp' + "$batchListFolder\$($_.Name)",

    The element is this:

    "tmp" + "$batchListFolder\$($_.Name)"

    What is the string "tmp" doing in  from of a pathname?


    Friday, September 8, 2017 12:58 AM
  • Well  thought that if "$batchListFolder\$($_.Name)" generates for example 123 then "tmp" + "$batchListFolder\$($_.Name)" will generate tmp123.

    But it clearly doesn't work like that.

    Friday, September 8, 2017 7:06 PM