【问题标题】:start-transcript causes script to fail in a background jobstart-transcript 导致脚本在后台作业中失败
【发布时间】:2010-10-01 01:43:16
【问题描述】:

我正在使用 start-job 创建一个 powershell 脚本作为后台作业,然后使用 start-transcript 记录它的输出。以下是两者的代码:

一个

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”行来进行一些测试,以确认这是播放的行。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    后台作业显然不支持 Start-Transcript。如果您执行 Receive-Job 来查看脚本输出,您可能会看到如下错误:

    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 - 它会捕获更多潜在错误。

    【讨论】:

    • 啊,解释了,谢谢你提供的信息和关于调试严格的提示。
    猜你喜欢
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    相关资源
    最近更新 更多