0

I'm trying to run two large sort processes simultaneously. So far, I've been unable to run one of these processes in the background.

Here's what I'm doing:

Get-Content $attfile |Sort-Object { 
  [datetime]::ParseExact($_.Substring(0, 23), 'dd/MM/yyyy HH:mm:ss.fff', $null) 
} | out-file -filepath "$new" -Encoding ascii 


Get-Content $navfile | Sort-Object { 
  [datetime]::ParseExact($_.Substring(0, 23), 'dd/MM/yyyy HH:mm:ss.fff', $null) 
} 2> $null | out-file -filepath "$navnew" -Encoding ascii

I would like to run the first sort in the background so that I don't have to wait for the first sort to finish. How can I do this? I find the powershell documentation of Start-Job very confusing.

Sam Alleman
  • 141
  • 7

1 Answers1

1

You can pass arguments to the job's scriptblock via the -ArgumentList parameter:

$job = Start-Job -ScriptBlock {
  param($inPath,$outPath)

  Get-Content $inPath |Sort-Object { 
    [datetime]::ParseExact($_.Substring(0, 23), 'dd/MM/yyyy HH:mm:ss.fff', $null) }
  } |Out-File $outPath
} -ArgumentList $attfile,$new

# do the other sort in foreground here ...

$job |Wait-Job

if($job.State -eq 'Completed'){
  # background sort succeeded, you can read back $new now
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • I had tried to use the -ArgumentList switch, but couldn't find information about how to use it in the script block. Thank you! Now... is there any way to suppress the job information output? – Sam Alleman Aug 25 '22 at 13:11
  • Nevermind... $job 2> $null | Wait-Job... Thanks! – Sam Alleman Aug 25 '22 at 13:19