start-transcript causes script to fail in a background job
I am creating a powershell script as a background job using start-job which then logs it's output using start-transcript. Below is the code of both:
a
start-job -filepath ./b.ps1 -ArgumentList 'test.txt'
wait-job *
b
param([parameter(Mandatory = $true)][string]$logfile)
Set-PSDebug -Strict
$ErrorActionPreference = 'Stop'
start-transcript $logfile
output of ./a.ps1
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
1 Job1 Running True localhost param...
1 Job1 Failed False localhost param..开发者_如何学JAVA.
2 Job2 Failed False localhost param...
Output of ./b.ps1 -log c:\test.txt
Transcript started, output file is test.txt
Transcript stopped, output file is C:\test.txt
I have also done some testing by setting "echo here" lines to confirm that is the line playing up.
Apparently Start-Transcript isn't supported in background jobs. If you do a Receive-Job to look at the script output you're likely to see an error like this:
This host does not support transcription.
+ CategoryInfo : NotImplemented: (:) [Start-Transcript], PSNotSupportedException
+ FullyQualifiedErrorId : NotSupported,Microsoft.PowerShell.Commands.StartTranscriptCommand
BTW in PowerShell 2.0 I recommend you switch from Set-PSDebug -strict
to Set-StrictMode -Version 2.0
- it will catch more potential errors.
精彩评论