gpt4 book ai didi

powershell - 从Job捕获详细流

转载 作者:行者123 更新时间:2023-12-02 23:28:26 24 4
gpt4 key购买 nike

我试图成为一名优秀的powerscript用户,并按照最佳实践使用Write-Verbose,但是我无法从正在运行的Job中获取Verbose流。

$Job = Start-Job -Name "Scanning Work Item" -ScriptBlock{
Write-Verbose "Write-Verbose"
Write-Host "Write-Host"
}

while ($Job.HasMoreData -or $Job.State -eq "Running") {
Receive-Job -Job $Job -Verbose
Start-Sleep -Seconds 1
}

输出是
Write-Host

请仅用经过测试的代码回答,因为我已经花了数小时尝试各种Powershell脚本排列。

最佳答案

首先,您没有得到任何冗长的输出,因为您尚未更改 session 的默认VerbosePreference。

至于在作业运行时读取详细输出,您可以分别从关联的子作业中读取每个输出流缓冲区,而无需执行Receive-job,并且在执行Receive-Job时也不会影响以后的输出,

$Job = Start-Job -Name "Scanning Work Item" -ScriptBlock{
$VerbosePreference = 'Continue'
Write-Verbose "Write-Verbose"
Write-Host "Write-Host"
Start-Sleep -Seconds 10
}

Start-sleep -Seconds 2

$Verbose = $Job.ChildJobs[0].verbose.readall()

$verbose

while ($Job.HasMoreData -or $Job.State -eq "Running") {
Receive-Job -Job $Job
Start-Sleep -Seconds 1
}


Write-Verbose
VERBOSE: Write-Verbose
Write-Host

关于powershell - 从Job捕获详细流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26026972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com