【问题标题】:PowerShell Jobs complete but do not create outputPowerShell 作业完成但不创建输出
【发布时间】:2020-02-17 07:39:03
【问题描述】:

我正试图让乔布斯工作并再次遇到问题。 编辑:意识到我需要更改作业脚本块中的每个变量以包含“使用”,所以$using:temp_cpu, $using:temp_cpu_cores, $using:temp_cpu_logical

即使使用using 修复,即使我正在等待输出文件 a) 存在并且 b) 大小为非零,但以下输出仍然随机导致空输出,但大约 1 次运行以下结果为空输出。

function test {
    $temp_cpu = "$($env:TEMP)\ps_temp_cpu.txt"
    $temp_cpu_cores = "$($env:TEMP)\ps_temp_cpu_cores.txt"
    $temp_cpu_logical = "$($env:TEMP)\ps_temp_cpu_logical.txt"
    rm -force $temp_cpu -EA silent ; rm -force $temp_cpu_cores -EA silent ; rm -force $temp_cpu_logical -EA silent
    $job_cpu         = Start-Job -ScriptBlock { (Get-WmiObject -Class Win32_Processor).Name > $using:temp_cpu }
    $job_cpu_cores   = Start-Job -ScriptBlock { (Get-WmiObject -Class Win32_Processor).NumberOfCores > $using:temp_cpu_cores }
    $job_cpu_logical = Start-Job -ScriptBlock { (Get-WmiObject -Class Win32_Processor).NumberOfLogicalProcessors > $using:temp_cpu_logical }
    Wait-Job $job_cpu, $job_cpu_cores, $job_cpu_logical
    while (!(Test-Path $temp_cpu)) { while ((Get-Item $temp_cpu -EA silent).length -eq 0kb) { Start-Sleep -Milliseconds 500 } }
    while (!(Test-Path $temp_cpu_cores)) { while ((Get-Item $temp_cpu_cores -EA silent).length -eq 0kb) { Start-Sleep -Milliseconds 500 } }
    while (!(Test-Path $temp_cpu_logical)) { while ((Get-Item $temp_cpu_logical -EA silent).length -eq 0kb) { Start-Sleep -Milliseconds 500 } }

    "CPU:               $(cat $temp_cpu)"
    "CPU Cores:         $(cat $temp_cpu_cores)"
    "CPU Logical:       $(cat $temp_cpu_logical)"
    rm -force $temp_cpu -EA silent ; rm -force $temp_cpu_cores -EA silent ; rm -force $temp_cpu_logical -EA silent
}

【问题讨论】:

  • Receive-job 获取输出。

标签: powershell parallel-processing jobs


【解决方案1】:

试试这个:

$job_cpu = Start-Job -ScriptBlock { (Get-WmiObject -Class Win32_Processor).Name }
Wait-Job $job_cpu
Receive-Job -Job $job_cpu -OutVariable job_cpu_output
$job_cpu_output > $temp_cpu

【讨论】:

  • 这是一个不错的选择。很高兴了解如何使用 Receive-Job,谢谢。我也意识到要让它像我一样工作,我必须在所有变量前面加上$using。不过,我喜欢 Receive-Job 的这种用法。 PowerShell 对此没有任何有用的错误(当然,作业在不同的进程中运行,所以没什么可看的)。
猜你喜欢
  • 2020-02-03
  • 2014-10-13
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 2017-01-20
  • 2018-09-06
  • 2019-11-06
相关资源
最近更新 更多