Write-Information在通过Start-Transcript转录的文件中不显示。

7
我正在使用PowerShell 5.1,试图确定为什么Write-Information消息不会显示在由Start-Transcript创建的传输日志中,除非我将$InformationPreference设置为SilentlyContinue。我想要在控制台中同时显示这些消息,并将它们写入日志文件。
我在这里查看了: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-5.1#informationpreference 然后我决定创建这个脚本来测试什么时候会被写入。请参阅Testing explicit behavior with transcripts -------------下面的首选项部分。
Clear-Host

$ErrorActionPreference = "Stop"

try {
    Write-Host "Starting transcript"
    Start-Transcript -Force -Path "$PSScriptRoot\default.txt"

    <#
        In PowerShell 5.1 the default behavior is as follows:
            $DebugPreference       = SilentlyContinue
            $InformationPreference = SilentlyContinue
            $ProgressPreference    = Continue
            $VerbosePreference     = SilentlyContinue

        See the following for more information:
        https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-5.1
    #>

    # I am not testing Write-Output as I am not worried about programmatic/pipeline stuff, just contextual messages for end-users or logging

    Write-Host "`nTesting default behavior with transcripts --------------------------------`n"

    # Setting these just in case I launch this script in a session where a previous script might have modified the preference variables
    $DebugPreference = "SilentlyContinue"
    $InformationPreference = "SilentlyContinue"
    $ProgressPreference = "Continue"
    $VerbosePreference = "SilentlyContinue"

    Write-Host "Calling Write-Host"
    Write-Debug "Calling Write-Debug"
    Write-Error "Calling Write-Error" -ErrorAction "Continue"
    Write-Information "Calling Write-Information"
    Write-Progress "Calling Write-Progress"
    Write-Verbose "Calling Write-Verbose"

    Stop-Transcript
    Start-Transcript -Force -Path "$PSScriptRoot\everything_continue.txt"

    Write-Host "`nTesting explicit behavior with transcripts --------------------------------`n"

    # Turn everything on
    $DebugPreference = "Continue"
    $InformationPreference = "Continue" # Setting this to SilentlyContinue makes it show up in the log but not the console. Setting this to 'Continue' makes it show up in the console but not the log.
    $ProgressPreference = "Continue"
    $VerbosePreference = "Continue"

    Write-Host "Calling Write-Host"
    Write-Debug "Calling Write-Debug"
    Write-Error "Calling Write-Error" -ErrorAction "Continue"
    Write-Information "Calling Write-Information"
    Write-Progress "Calling Write-Progress"
    Write-Verbose "Calling Write-Verbose"

    Stop-Transcript

    Write-Host "`nResults -------------------------------------------------------------------`n"

    # See what actually gets captured and written by the transcriber

    $messageTypes = @("Write-Debug", "Write-Error", "Write-Host", "Write-Information", "Write-Verbose")

    Write-Host "Default" -ForegroundColor Cyan
    $lines = Get-Content "$PSScriptRoot\default.txt"
    foreach ($message in $messageTypes) {
        if ($lines -like "*Calling $message*") {
            Write-Host "  $message PRESENT" -ForegroundColor Green
        }
        else {
            Write-Host "  $message MISSING" -ForegroundColor Red
        }
    }

    Write-Host "Everything Continue" -ForegroundColor Cyan
    $lines = Get-Content "$PSScriptRoot\everything_continue.txt"
    foreach ($message in $messageTypes) {
        if ($lines -like "*Calling $message*") {
            Write-Host "  $message PRESENT" -ForegroundColor Green
        }
        else {
            Write-Host "  $message MISSING" -ForegroundColor Red
        }
    }
}
catch {
    Write-Host "----------------------------------------------------------------------------------------------------"
    Write-Host $_.Exception
    Write-Host $_.ScriptStackTrace
    Write-Host "----------------------------------------------------------------------------------------------------"

    try { Stop-Transcript } catch { }

    throw $_
}

我正在尝试确定为什么除非我将$InformationPreference设置为SilentlyContinue,否则由Start-Transcript创建的传输日志中不会显示Write-Information消息。因为InformationRecord处理的编写方式是要么显示,要么记录,但不能同时进行。 - user4003407
那我是不是应该退而使用Write-Host呢? - Socketed Chinchilla
在PowerShell 5中,Write-Host只是在幕后使用Write-Information - Maximilian Burszley
1个回答

12
您所看到的是Windows PowerShell(版本为v5.1.17134.590)中的一个错误,已在PowerShell Core(至少v6.1.0)中得到修复(但仍存在其他与记录相关的问题,请参见this GitHub issue)。请注意,我鼓励您在Windows PowerShell UserVoice 论坛上报告此问题(请注意,PowerShell GitHub 存储库issues forum 仅用于PowerShell Core中也存在的错误)。以下是验证您的PowerShell版本是否存在此错误的方法:创建一个包含以下代码的脚本并运行它:
'--- Direct output'

$null = Start-Transcript ($tempFile = [io.path]::GetTempFileName())

    # Note that 'SilentlyContinue' is also the default value.
    $InformationPreference = 'SilentlyContinue'

    # Produces no output.
    Write-Information '1-information' 

    # Prints '2-Information' to the console.
    Write-Information '2-information' -InformationAction Continue

$null = Stop-Transcript

'--- Write-Information output transcribed:'

Select-String '-information' $tempFile | Select-Object -ExpandProperty Line

Remove-Item $tempFile

如果存在这个bug(Windows PowerShell),你将看到:

--- Direct output
2-information
--- Write-Information output transcribed:
INFO: 1-information

那就是,产生了与预期行为相反的结果:记录了不应该记录的呼叫(因为它没有产生输出),而没有记录应该记录的呼叫。此外,记录的输出前缀为INFO: ,这是一个不一致性,也已在PowerShell Core中修复。没有完全的解决方法,除非您在需要记录转录的情况下使用Write-Host调用,但这样的调用将被无条件地记录,而不管偏好变量$InformationPreference的值如何(虽然Write-Host正式提供了一个-InformationAction通用参数,但它被忽略)。

有了错误修复(PowerShell Core),您将看到:

--- Direct output
2-information
--- Write-Information output transcribed:
2-information

转录现在与直接输出一致。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接