写信息不会显示在由Start-Transcript转录的文件中

时间:2019-03-15 22:40:12

标签: powershell logging cmdlets powershell-v5.1

我正在使用PowerShell 5.1,并且试图确定为什么Write-Information消息不会显示在Start-Transcript创建的记录日志中,除非我将$InformationPreference设置为SilentlyContinue 。我想同时在控制台中显示消息,并将其写入日志文件。

我看着这里: https://docs.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://docs.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 $_
}

1 个答案:

答案 0 :(得分:1)

您看到的是 Windows PowerShell 中的 bug (自v5.1.17134.590起),已经已在PowerShell Core 中进行了修复(至少在v6.1.0及更高版本中-尽管与其他脚本有关的问题仍然存在;参见this GitHub issue)。

我建议您在Windows PowerShell UserVoice论坛中进行报告(请注意,PowerShell GitHub-repo 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

出现错误 (Windows PowerShell),您将看到:

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

也就是说,发生了预期行为的 相反 :该笔录记录了它不应该收到的呼叫(因为它没有产生输出),但是它没有t记录它应该有的那个。

此外,记录的输出以INFO: 为前缀,这是一个不一致问题,在PowerShell Core中也已解决。

没有没有完整的解决方法,不同的是您可以在需要执行输出记录的情况下使用 Write-Host 调用记录中-但无论喜好变量$InformationPreference的值如何,此类呼叫都会无条件记录 (而Write-Host 正式< / em>提供一个-InformationAction通用参数,它被忽略)。


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

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

现在,笔录与直接输出一致。