PowerShell 作业完成但未创建输出

PowerShell Jobs complete but do not create output

我正试图让乔布斯工作,但又遇到了问题。 编辑:意识到我需要更改 Job ScriptBlocks 中的每个变量以包含 "using",因此 $using:temp_cpu, $using:temp_cpu_cores, $using:temp_cpu_logical

即使使用 using 修复,下面的输出仍然随机导致空输出,即使我正在等待输出文件 a) 存在,并且 b) 是非零大小,但大约以下 3 次运行中有 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
}

试试这个:

$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