start-transcript 导致脚本在后台作业中失败 [英] start-transcript causes script to fail in a background job
问题描述
我正在使用 start-job 创建一个 powershell 脚本作为后台作业,然后使用 start-transcript 记录它的输出.下面是两者的代码:
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:
一
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
./a.ps1 的输出
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
1 Job1 Running True localhost param...
1 Job1 Failed False localhost param...
2 Job2 Failed False localhost param...
./b.ps1 -log c:\test.txt 的输出
Transcript started, output file is test.txt
Transcript stopped, output file is C:\test.txt
我还通过设置echo here"行进行了一些测试,以确认该行正在播放.
I have also done some testing by setting "echo here" lines to confirm that is the line playing up.
推荐答案
显然后台作业不支持 Start-Transcript.如果您执行 Receive-Job 以查看脚本输出,您可能会看到如下错误:
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
顺便说一句,PowerShell 2.0 中我建议您从 Set-PSDebug -strict
切换到 Set-StrictMode -Version 2.0
- 它会捕获更多潜在错误.
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.
这篇关于start-transcript 导致脚本在后台作业中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!