5

I am trying to get the job details without outputting the data to the screen. However, regardless of what option I try, the job logs always get sent to the console. Any ideas on how to save the logs in a variable or file without outputting that data to console?

Receive-Job -Id $id -Keep -ErrorAction Continue > C:\Temp\Transcript-$VM.txt

$info = Receive-Job -Id $id -Keep -ErrorAction Continue
7
  • 1
    The job must have a Write-Host or Out-Host statement in the script block. You should remove that if so. You could also set the $InformationPreference variable to 'Ignore' in the scriptblock if you are on PowerShell v5 or later, but it may have unintended consequences. The -InformationAction 'Ignore' on Write-Host does the same thing with a specific command and doesn't impact the environment settings. Commented Nov 11, 2019 at 18:22
  • The job does have Write-Host commands in it and they can be seen when running the Receive-Job command as it outputs this info to the screen. Commented Nov 11, 2019 at 18:57
  • Also I tried all the options for $InformationPreference ('Ignore') is not one of them see below link, but it still displays content to the screen, or stops the script completely. learn.microsoft.com/en-us/powershell/module/… Commented Nov 11, 2019 at 19:02
  • What is your powershell version? Commented Nov 11, 2019 at 19:04
  • PSVersion: 5.1.18362.145 Commented Nov 11, 2019 at 19:04

2 Answers 2

4

You state that your job uses Write-Host output and that you're running Windows PowerShell v5.1.

In order to also capture Write-Host output - which in v5+ is sent to the information stream (stream number 6) - use redirection 6>&1:

# Capture both success output and information-stream output
# (Write-Host) output in $info.
$info = Receive-Job -Id $id -Keep -ErrorAction Continue 6>&1

Unfortunately, due to a known bug, you'll still get console output as well (bug is still present in PowerShell Core 7.0.0-preview.5).

Catch-all redirection *>&1 normally routes all streams through the success output stream.

Unfortunately, due to the bug linked to above, the following streams cannot be captured or redirected at all when using background jobs or remoting:

  • verbose messages (4)
  • debug messages (5)

The only workaround is to capture the streams inside the job and save them to a file from there, and then access the files from the caller later.
Of course, this requires that you have control over how the jobs are created.

A simplified example:

# Redirect all output streams *inside* the job to a file...
Start-Job { 
 & { 
   # The job's commands go here.
   # Note that for any *verbose* output to be captured,
   # verbose output must explicitly turned on, such as with
   # the -Verbose common parameter here.
   # You can also set $VerbosePreference = 'Continue', which 
   # cmdlets (including advanced functions/scripts) will honor.
   'success'; write-verbose -Verbose 'verbose'; write-host 'host' 
 } *> $HOME/out.txt 
} | Receive-Job -Wait -AutoRemove
# ... then read the resulting file.
Get-Content $HOME/out.txt

Note that I've used a full path as the redirection target, because, unfortunately, in v6- versions of PowerShell script blocks executed in background jobs do not inherit the caller's current location. This will change in PowerShell Core v7.0.

Sign up to request clarification or add additional context in comments.

Comments

-1

Try placing it in a pipeline, and see if that works:

Receive-Job -Id $id -Keep -ErrorAction Continue | Set-Content 'C:\Temp\Transcript-$VM.txt'

4 Comments

Output in the file is the below. Also still shows the logs on screen. Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.PSResourceGroupDeployment Microsoft.Azure.Commands.Compute.Models.PSComputeLongRunningOperation Microsoft.Azure.Commands.Compute.Models.PSComputeLongRunningOperation
This bug report explains why that won't help.
Also, > does involve a pipeline, behind the scenes; it is like implicit use of | Out-File, so what you're suggesting fundamentally couldn't be expected to make a difference.
I misunderstood the question. I thought he was attempting to hide the actual output of the job object and send to file, not gather the output of the DataStream of the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.