繁体   English   中英

如何在 Powershell 的后台任务中运行带有变量的管道?

[英]How can I run a pipeline with variables in a background task in Powershell?

我正在尝试同时运行两个大型排序过程。 到目前为止,我一直无法在后台运行这些进程之一。

这就是我正在做的事情:

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

我想在后台运行第一个排序,这样我就不必等待第一个排序完成。 我怎样才能做到这一点? 我发现 Start-Job 的 powershell 文档非常混乱。

您可以通过-ArgumentList参数将 arguments 传递给作业的脚本块:

$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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM