Adeel ASIF
Adeel ASIF

Reputation: 3534

Receive-Job output to a file

how can i output the receive-job result to a file?

I tried this but it doesn't work :

$log = "C:\springfield\citrix\CitrixAutomation.log"
Get-Job | Receive-Job | Out-File $log

I also tried to save the output of Get-Job | Receive-Job to a variable, but it doesn't work..

$log = "C:\springfield\citrix\CitrixAutomation.log"
$getjobarr = @()
Function LogWrite
{
   Param ([array]$logstring)
   $logstring | Out-File $log -Append
}

$getjobarr += Get-Job | Receive-Job

LogWrite $getjobarr

I think that Get-Job and Receive-Job can only output to console only, so how can i achieve it?

Thanks for your help

Upvotes: 3

Views: 6976

Answers (2)

R_C_III
R_C_III

Reputation: 13

To add to Keith's answer:
I spent a day banging my head against this for a complex script with a dozen functions. My Out-File would only output to Console, or to Console & txt file, but I could never hide it from the Console.

My fix had two steps:
1. *>&1 on the Receive-Job cmdlet

$logfile = "$env:LOCALAPPDATA\temp\ScriptResult.txt"
   Get-Job | foreach-object {
   Receive-Job -name $_.name *>&1 >> $logfile
   "`r`n=====================================" | Out-File -FilePath $logfile -Append
   }

The *>&1 was key. It forced all data streams (Information and Error) into the output so everything appeared in in the log file
(You can pipe to Out-File instead of >> if you would prefer to write it out.)
2. Find & Replace all Write-Host cmdlets with Write-Output
Most of my functions had Write-Host explanations for error messages. Even when I used *>&1 to redirect the stream it caused the output to show on the Console. Replacing with Write-Output fixed that.

Thanks Keith for the critical info I could not find anywhere else! :D
(Works on PSVersion v5.1, Win10)

Upvotes: 1

Keith Hill
Keith Hill

Reputation: 201662

Job "output" can be redirected to a file e.g.:

PS> Start-Job {Get-ChildItem C:\users\keith}

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
2      Job2            BackgroundJob   Running       True            localhost            Get-ChildItem C:\users...


PS> Receive-Job -id 2 | Out-File job.log
PS> gc .\job.log


    Directory: C:\users\keith


Mode           LastWriteTime       Length Name
----           -------------       ------ ----
d----     1/21/2014  8:24 PM        <DIR> .ssh
d----      9/9/2014 10:00 PM        <DIR> Bin
d-r--     9/11/2014  9:20 PM        <DIR> Contacts
d-r--     9/11/2014  9:20 PM        <DIR> Desktop

If whatever you're running in the job writes to host then yeah, you're hosed. If you control that, use write-output instead of write-host. Also, make sure you wait until each job is complete before receiving its output unless you can sit in a loop wait until the job state changes to Completed or Failed. You can use Wait-Job to wait for a job to finish before asking for its output.

Upvotes: 5

Related Questions