Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取使用Register ObjectEvent注册的Powershell作业的输出_Powershell - Fatal编程技术网

获取使用Register ObjectEvent注册的Powershell作业的输出

获取使用Register ObjectEvent注册的Powershell作业的输出,powershell,Powershell,我有一个脚本正在执行,但没有返回输出 function code-Build-RepoWeb { $job = Start-Job { code-Build-Component RepoWeb; Start-Sleep -seconds 5 } Register-ObjectEvent $job -EventName StateChanged ` -SourceIdentifier JobStateChanged ` -Action {$jobInfo =

我有一个脚本正在执行,但没有返回输出

function code-Build-RepoWeb
{
     $job = Start-Job { code-Build-Component RepoWeb; Start-Sleep -seconds 5 }

     Register-ObjectEvent $job -EventName StateChanged `
    -SourceIdentifier JobStateChanged `
    -Action {$jobInfo = Receive-Job -Keep $Sender; Write-Host $jobInfo }
 }
我不是Powershell的专家,在问问题之前先阅读一些文档


谢谢

无需注册活动,以下是您的注册方法:

 Start-Job { ... } | Wait-Job | Receive-Job -Keep
如果您需要非阻塞命令。这将创建一个全局变量来保存作业的结果:

$job = Start-Job { Start-Sleep -Seconds 5; 1..5 }

$null = Register-ObjectEvent $job -EventName StateChanged -SourceIdentifier JobEnd -Action {

    if($sender.State -eq 'Completed')
    {
        $global:jobInfo = Receive-Job $job        
    } 
}   

$jobInfo

无需注册活动,以下是您可以注册的方法:

 Start-Job { ... } | Wait-Job | Receive-Job -Keep
如果您需要非阻塞命令。这将创建一个全局变量来保存作业的结果:

$job = Start-Job { Start-Sleep -Seconds 5; 1..5 }

$null = Register-ObjectEvent $job -EventName StateChanged -SourceIdentifier JobEnd -Action {

    if($sender.State -eq 'Completed')
    {
        $global:jobInfo = Receive-Job $job        
    } 
}   

$jobInfo

似乎您应该使用
等待作业
而不是侦听事件<代码>等待作业首先,然后将完成
$Job
,您可以使用
接收作业
。似乎您应该使用
等待作业
,而不是侦听事件<代码>先等待作业,然后将完成
$Job
,您可以使用
接收作业
。是的,就是这样<代码>-保留是可选的。不要忘记在作业完成后删除作业。忘记说此调用必须是非阻塞的,这就是我注册事件的原因。是的,正是这个<代码>-保留是可选的。不要忘记在作业完成后删除作业。忘记说此调用必须是非阻塞的,这就是我注册事件的原因。