Powershell: get output from Receive-Job

前端 未结 7 1581
误落风尘
误落风尘 2020-12-09 08:57

I have a collection of jobs that are running. When they complete I use receive-job and it writes the output to the screen. I\'d like to take that output and log it to a file

相关标签:
7条回答
  • 2020-12-09 09:10

    When you receive the results of a job those results are deleted such that further calls to Receive-Job will have nothing to retrieve (assuming the job is completed). If you don't want Receive-Job to delete the results use the -Keep switch. Now this doesn't explain why your first call to Receive-Job isn't outputting anything to the log file ... unless there is a part of the script you're not showing that is doing a Receive-Job before this.

    0 讨论(0)
  • 2020-12-09 09:13

    You could pipe your Get-Process to Format-Table, so you can see all of the output.

        Get-Process -Name "explorer" | format-Table -hidetableheaders -AutoSize
    

    And you could use something like this at the end of the script...

        Get-job | Receive-Job 2>&1 >> c:\path\to\your\log\file.log
    

    The "2>&1 >>" with grab all output from Get-Job | Receive-Job

    0 讨论(0)
  • 2020-12-09 09:14

    I know this is old. I don't use jobs very often so I was obviously having the same problem when I stumbled on this thread. Anyway, I arrived at a different solution so I thought I'd throw it out there.

    Also in PowerShell 5.1 I simply referenced the properties on the Bob Object(s) In my case this looked like:

    $Jobs.ChildJobs.Output | Out-File <LogFile> -Append
    

    It looks like all the streams are recorded in this way. Simple echoes go to the Output. Write-Host commands go to Information Stream.

    Output        : {}
    Error         : {}
    Progress      : {}
    Verbose       : {}
    Debug         : {}
    Warning       : {}
    Information   : {}
    

    At any rate I hope that's helpful to someone.

    0 讨论(0)
  • 2020-12-09 09:15

    A simple solution to the problem. Write-Verbose "blabla" -Verbose


    $s = New-PSSession -ComputerName "Computer"
    $j = Invoke-Command -Session $s  -ScriptBlock { Write-Verbose "Message" -Verbose } -AsJob
    Wait-Job $j
    
    $Child = $j.ChildJobs[0]
    $Child.Verbose | out-file d:/job.log -append
    
    0 讨论(0)
  • 2020-12-09 09:23

    If the job uses Write-Host to produce output, Receive-Job returns $null, but the results get written to the host. However, if the job uses Write-Output to produce output in lieu of Write-Host, Receive-Job returns a string array [string[]] of the job output.

    To demonstrate, enter this innocuous code at the PowerShell prompt:

    $job = Start-Job -ScriptBlock {
        [int] $counter = 0
        while ($counter -lt 10) {
            Write-Output "Counter = $counter."
            Start-Sleep -Seconds 5
            $counter++
        }
    }
    

    Wait about 20-30 seconds for the job to produce some output, then enter this code:

    $result = Receive-Job -Job $job
    $result.Count
    $result
    $result | Get-Member
    

    The $result object contains the strings produced by the job.

    0 讨论(0)
  • 2020-12-09 09:32

    I found soulution how to receive Write-Host results to variable with Receive-Job CMDlet, it's working on Powershell 5.1:

    $var = Receive-Job $job 6>&1
    
    0 讨论(0)
提交回复
热议问题